2 ** Copyright 2007, The Android Open Source Project
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
8 ** http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
24 #include <hardware/gralloc1.h>
27 #include <EGL/eglext.h>
29 #include <android/hardware_buffer.h>
30 #include <private/android/AHardwareBufferHelpers.h>
32 #include <cutils/compiler.h>
33 #include <cutils/properties.h>
36 #include <condition_variable>
39 #include <unordered_map>
43 #include "../egl_impl.h"
45 #include "egl_display.h"
46 #include "egl_object.h"
48 #include "egl_trace.h"
50 using namespace android;
52 // ----------------------------------------------------------------------------
56 using nsecs_t = int64_t;
58 struct extention_map_t {
60 __eglMustCastToProperFunctionPointerType address;
64 * This is the list of EGL extensions exposed to applications.
66 * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
67 * wrapper and are always available.
69 * The rest (gExtensionString) depend on support in the EGL driver, and are
70 * only available if the driver supports them. However, some of these must be
71 * supported because they are used by the Android system itself; these are
72 * listed as mandatory below and are required by the CDD. The system *assumes*
73 * the mandatory extensions are present and may not function properly if some
76 * NOTE: Both strings MUST have a single space as the last character.
79 extern char const * const gBuiltinExtensionString;
80 extern char const * const gExtensionString;
83 char const * const gBuiltinExtensionString =
84 "EGL_KHR_get_all_proc_addresses "
85 "EGL_ANDROID_presentation_time "
86 "EGL_KHR_swap_buffers_with_damage "
87 "EGL_ANDROID_get_native_client_buffer "
88 "EGL_ANDROID_front_buffer_auto_refresh "
89 "EGL_ANDROID_get_frame_timestamps "
90 "EGL_EXT_gl_colorspace_scrgb_linear "
91 "EGL_EXT_gl_colorspace_display_p3_linear "
92 "EGL_EXT_gl_colorspace_display_p3 "
95 char const * const gExtensionString =
96 "EGL_KHR_image " // mandatory
97 "EGL_KHR_image_base " // mandatory
98 "EGL_KHR_image_pixmap "
99 "EGL_KHR_lock_surface "
100 "EGL_KHR_gl_colorspace "
101 "EGL_KHR_gl_texture_2D_image "
102 "EGL_KHR_gl_texture_3D_image "
103 "EGL_KHR_gl_texture_cubemap_image "
104 "EGL_KHR_gl_renderbuffer_image "
105 "EGL_KHR_reusable_sync "
106 "EGL_KHR_fence_sync "
107 "EGL_KHR_create_context "
108 "EGL_KHR_config_attribs "
109 "EGL_KHR_surfaceless_context "
111 "EGL_KHR_stream_fifo "
112 "EGL_KHR_stream_producer_eglsurface "
113 "EGL_KHR_stream_consumer_gltexture "
114 "EGL_KHR_stream_cross_process_fd "
115 "EGL_EXT_create_context_robustness "
116 "EGL_NV_system_time "
117 "EGL_ANDROID_image_native_buffer " // mandatory
118 "EGL_KHR_wait_sync " // strongly recommended
119 "EGL_ANDROID_recordable " // mandatory
120 "EGL_KHR_partial_update " // strongly recommended
121 "EGL_EXT_pixel_format_float "
122 "EGL_EXT_buffer_age " // strongly recommended with partial_update
123 "EGL_KHR_create_context_no_error "
124 "EGL_KHR_mutable_render_buffer "
125 "EGL_EXT_yuv_surface "
126 "EGL_EXT_protected_content "
127 "EGL_IMG_context_priority "
128 "EGL_KHR_no_config_context "
132 // extensions not exposed to applications but used by the ANDROID system
133 // "EGL_ANDROID_blob_cache " // strongly recommended
134 // "EGL_IMG_hibernate_process " // optional
135 // "EGL_ANDROID_native_fence_sync " // strongly recommended
136 // "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
137 // "EGL_ANDROID_image_crop " // optional
140 * EGL Extensions entry-points exposed to 3rd party applications
141 * (keep in sync with gExtensionString above)
144 static const extention_map_t sExtensionMap[] = {
145 // EGL_KHR_lock_surface
146 { "eglLockSurfaceKHR",
147 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
148 { "eglUnlockSurfaceKHR",
149 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
151 // EGL_KHR_image, EGL_KHR_image_base
152 { "eglCreateImageKHR",
153 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
154 { "eglDestroyImageKHR",
155 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
157 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
158 { "eglCreateSyncKHR",
159 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
160 { "eglDestroySyncKHR",
161 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
162 { "eglClientWaitSyncKHR",
163 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
164 { "eglSignalSyncKHR",
165 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
166 { "eglGetSyncAttribKHR",
167 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
169 // EGL_NV_system_time
170 { "eglGetSystemTimeFrequencyNV",
171 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
172 { "eglGetSystemTimeNV",
173 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
177 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
179 // EGL_ANDROID_presentation_time
180 { "eglPresentationTimeANDROID",
181 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
183 // EGL_KHR_swap_buffers_with_damage
184 { "eglSwapBuffersWithDamageKHR",
185 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
187 // EGL_ANDROID_get_native_client_buffer
188 { "eglGetNativeClientBufferANDROID",
189 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
191 // EGL_KHR_partial_update
192 { "eglSetDamageRegionKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
195 { "eglCreateStreamKHR",
196 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
197 { "eglDestroyStreamKHR",
198 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
199 { "eglStreamAttribKHR",
200 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
201 { "eglQueryStreamKHR",
202 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
203 { "eglQueryStreamu64KHR",
204 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
205 { "eglQueryStreamTimeKHR",
206 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
207 { "eglCreateStreamProducerSurfaceKHR",
208 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
209 { "eglStreamConsumerGLTextureExternalKHR",
210 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
211 { "eglStreamConsumerAcquireKHR",
212 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
213 { "eglStreamConsumerReleaseKHR",
214 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
215 { "eglGetStreamFileDescriptorKHR",
216 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
217 { "eglCreateStreamFromFileDescriptorKHR",
218 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
220 // EGL_ANDROID_get_frame_timestamps
221 { "eglGetNextFrameIdANDROID",
222 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
223 { "eglGetCompositorTimingANDROID",
224 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
225 { "eglGetCompositorTimingSupportedANDROID",
226 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
227 { "eglGetFrameTimestampsANDROID",
228 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
229 { "eglGetFrameTimestampSupportedANDROID",
230 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
232 // EGL_ANDROID_native_fence_sync
233 { "eglDupNativeFenceFDANDROID",
234 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
238 * These extensions entry-points should not be exposed to applications.
239 * They're used internally by the Android EGL layer.
241 #define FILTER_EXTENSIONS(procname) \
242 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
243 !strcmp((procname), "eglHibernateProcessIMG") || \
244 !strcmp((procname), "eglAwakenProcessIMG"))
248 // accesses protected by sExtensionMapMutex
249 static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
251 static int sGLExtentionSlot = 0;
252 static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
254 static void(*findProcAddress(const char* name,
255 const extention_map_t* map, size_t n))() {
256 for (uint32_t i=0 ; i<n ; i++) {
257 if (!strcmp(name, map[i].name)) {
258 return map[i].address;
264 // ----------------------------------------------------------------------------
266 extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
267 extern EGLBoolean egl_init_drivers();
268 extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
269 extern gl_hooks_t gHooksTrace;
271 } // namespace android;
274 // ----------------------------------------------------------------------------
276 static inline void clearError() { egl_tls_t::clearError(); }
277 static inline EGLContext getContext() { return egl_tls_t::getContext(); }
279 // ----------------------------------------------------------------------------
281 EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
286 uintptr_t index = reinterpret_cast<uintptr_t>(display);
287 if (index >= NUM_DISPLAYS) {
288 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
291 if (egl_init_drivers() == EGL_FALSE) {
292 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
295 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
307 egl_display_ptr dp = get_display(dpy);
308 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
310 EGLBoolean res = dp->initialize(major, minor);
315 EGLBoolean eglTerminate(EGLDisplay dpy)
317 // NOTE: don't unload the drivers b/c some APIs can be called
318 // after eglTerminate() has been called. eglTerminate() only
319 // terminates an EGLDisplay, not a EGL itself.
323 egl_display_ptr dp = get_display(dpy);
324 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
326 EGLBoolean res = dp->terminate();
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 EGLBoolean eglGetConfigs( EGLDisplay dpy,
337 EGLint config_size, EGLint *num_config)
341 const egl_display_ptr dp = validate_display(dpy);
342 if (!dp) return EGL_FALSE;
345 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
348 EGLBoolean res = EGL_FALSE;
351 egl_connection_t* const cnx = &gEGLImpl;
353 res = cnx->egl.eglGetConfigs(
354 dp->disp.dpy, configs, config_size, num_config);
360 EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
361 EGLConfig *configs, EGLint config_size,
366 const egl_display_ptr dp = validate_display(dpy);
367 if (!dp) return EGL_FALSE;
370 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
373 EGLBoolean res = EGL_FALSE;
376 egl_connection_t* const cnx = &gEGLImpl;
379 char value[PROPERTY_VALUE_MAX];
380 property_get("debug.egl.force_msaa", value, "false");
382 if (!strcmp(value, "true")) {
383 size_t attribCount = 0;
384 EGLint attrib = attrib_list[0];
386 // Only enable MSAA if the context is OpenGL ES 2.0 and
387 // if no caveat is requested
388 const EGLint *attribRendererable = NULL;
389 const EGLint *attribCaveat = NULL;
391 // Count the number of attributes and look for
392 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
393 while (attrib != EGL_NONE) {
394 attrib = attrib_list[attribCount];
396 case EGL_RENDERABLE_TYPE:
397 attribRendererable = &attrib_list[attribCount];
399 case EGL_CONFIG_CAVEAT:
400 attribCaveat = &attrib_list[attribCount];
408 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
409 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
411 // Insert 2 extra attributes to force-enable MSAA 4x
412 EGLint aaAttribs[attribCount + 4];
413 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
415 aaAttribs[2] = EGL_SAMPLES;
418 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
421 EGLBoolean resAA = cnx->egl.eglChooseConfig(
422 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
424 if (resAA == EGL_TRUE && numConfigAA > 0) {
425 ALOGD("Enabling MSAA 4x");
426 *num_config = numConfigAA;
433 res = cnx->egl.eglChooseConfig(
434 dp->disp.dpy, attrib_list, configs, config_size, num_config);
439 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
440 EGLint attribute, EGLint *value)
444 egl_connection_t* cnx = NULL;
445 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
446 if (!dp) return EGL_FALSE;
448 return cnx->egl.eglGetConfigAttrib(
449 dp->disp.dpy, config, attribute, value);
452 // ----------------------------------------------------------------------------
454 // ----------------------------------------------------------------------------
456 // Turn linear formats into corresponding sRGB formats when colorspace is
457 // EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
458 // formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
459 // the modification isn't possible, the original dataSpace is returned.
460 static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
462 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
463 return HAL_DATASPACE_SRGB_LINEAR;
464 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
465 return HAL_DATASPACE_SRGB;
466 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
467 return HAL_DATASPACE_DISPLAY_P3;
468 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
469 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
470 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
471 return HAL_DATASPACE_V0_SCRGB_LINEAR;
476 static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
477 EGLint& colorSpace, android_dataspace& dataSpace) {
478 colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
479 dataSpace = HAL_DATASPACE_UNKNOWN;
480 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
481 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
482 if (*attr == EGL_GL_COLORSPACE_KHR) {
483 colorSpace = attr[1];
485 // Verify that color space is allowed
486 if (colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
487 colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR) {
489 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_linear &&
490 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_linear")) {
492 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_pq &&
493 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_pq")) {
495 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT &&
496 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb_linear")) {
498 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT &&
499 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3_linear")) {
501 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT &&
502 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3")) {
508 // Only change the dataSpace from default if the application
509 // has explicitly set the color space with a EGL_GL_COLORSPACE_KHR attribute.
510 dataSpace = modifyBufferDataspace(dataSpace, colorSpace);
517 EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
518 NativeWindowType window,
519 const EGLint *attrib_list)
523 egl_connection_t* cnx = NULL;
524 egl_display_ptr dp = validate_display_connection(dpy, cnx);
526 EGLDisplay iDpy = dp->disp.dpy;
529 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
533 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
535 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
538 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
540 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
541 "failed (%#x) (already connected to another API?)",
543 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
546 // Set the native window's buffers format to match what this config requests.
547 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
548 // of our native format. So if sRGB gamma is requested, we have to
549 // modify the EGLconfig's format before setting the native window's
552 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
553 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_COLOR_COMPONENT_TYPE_EXT,
558 android_dataspace dataSpace;
562 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE, &r);
563 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
564 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE, &b);
565 cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
566 EGLint colorDepth = r + g + b;
569 if (colorDepth <= 16) {
570 format = HAL_PIXEL_FORMAT_RGB_565;
572 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
573 if (colorDepth > 24) {
574 format = HAL_PIXEL_FORMAT_RGBA_1010102;
576 format = HAL_PIXEL_FORMAT_RGBX_8888;
579 format = HAL_PIXEL_FORMAT_RGBA_FP16;
583 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
584 if (colorDepth > 24) {
585 format = HAL_PIXEL_FORMAT_RGBA_1010102;
587 format = HAL_PIXEL_FORMAT_RGBA_8888;
590 format = HAL_PIXEL_FORMAT_RGBA_FP16;
594 // now select a corresponding sRGB format if needed
595 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
596 ALOGE("error invalid colorspace: %d", colorSpace);
597 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
601 int err = native_window_set_buffers_format(window, format);
603 ALOGE("error setting native window pixel format: %s (%d)",
604 strerror(-err), err);
605 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
606 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
610 if (dataSpace != 0) {
611 int err = native_window_set_buffers_data_space(window, dataSpace);
613 ALOGE("error setting native window pixel dataSpace: %s (%d)",
614 strerror(-err), err);
615 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
616 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
620 // the EGL spec requires that a new EGLSurface default to swap interval
621 // 1, so explicitly set that on the window here.
622 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
623 anw->setSwapInterval(anw, 1);
625 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
626 iDpy, config, window, attrib_list);
627 if (surface != EGL_NO_SURFACE) {
629 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
633 // EGLSurface creation failed
634 native_window_set_buffers_format(window, 0);
635 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
637 return EGL_NO_SURFACE;
640 EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
641 NativePixmapType pixmap,
642 const EGLint *attrib_list)
646 egl_connection_t* cnx = NULL;
647 egl_display_ptr dp = validate_display_connection(dpy, cnx);
649 android_dataspace dataSpace;
651 // now select a corresponding sRGB format if needed
652 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
653 ALOGE("error invalid colorspace: %d", colorSpace);
654 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
657 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
658 dp->disp.dpy, config, pixmap, attrib_list);
659 if (surface != EGL_NO_SURFACE) {
660 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
664 return EGL_NO_SURFACE;
667 EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
668 const EGLint *attrib_list)
672 egl_connection_t* cnx = NULL;
673 egl_display_ptr dp = validate_display_connection(dpy, cnx);
675 android_dataspace dataSpace;
677 // now select a corresponding sRGB format if needed
678 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
679 ALOGE("error invalid colorspace: %d", colorSpace);
680 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
683 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
684 dp->disp.dpy, config, attrib_list);
685 if (surface != EGL_NO_SURFACE) {
686 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
690 return EGL_NO_SURFACE;
693 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
697 const egl_display_ptr dp = validate_display(dpy);
698 if (!dp) return EGL_FALSE;
700 SurfaceRef _s(dp.get(), surface);
702 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
704 egl_surface_t * const s = get_surface(surface);
705 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
706 if (result == EGL_TRUE) {
712 EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
713 EGLint attribute, EGLint *value)
717 const egl_display_ptr dp = validate_display(dpy);
718 if (!dp) return EGL_FALSE;
720 SurfaceRef _s(dp.get(), surface);
722 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
724 egl_surface_t const * const s = get_surface(surface);
725 if (attribute == EGL_GL_COLORSPACE_KHR) {
726 *value = s->getColorSpace();
729 return s->cnx->egl.eglQuerySurface(
730 dp->disp.dpy, s->surface, attribute, value);
733 void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
737 const egl_display_ptr dp = validate_display(dpy);
742 SurfaceRef _s(dp.get(), surface);
744 setError(EGL_BAD_SURFACE, EGL_FALSE);
748 // ----------------------------------------------------------------------------
750 // ----------------------------------------------------------------------------
752 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
753 EGLContext share_list, const EGLint *attrib_list)
757 egl_connection_t* cnx = NULL;
758 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
760 if (share_list != EGL_NO_CONTEXT) {
761 if (!ContextRef(dp.get(), share_list).get()) {
762 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
764 egl_context_t* const c = get_context(share_list);
765 share_list = c->context;
767 EGLContext context = cnx->egl.eglCreateContext(
768 dp->disp.dpy, config, share_list, attrib_list);
769 if (context != EGL_NO_CONTEXT) {
770 // figure out if it's a GLESv1 or GLESv2
773 while (*attrib_list != EGL_NONE) {
774 GLint attr = *attrib_list++;
775 GLint value = *attrib_list++;
776 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
778 version = egl_connection_t::GLESv1_INDEX;
779 } else if (value == 2 || value == 3) {
780 version = egl_connection_t::GLESv2_INDEX;
785 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
790 return EGL_NO_CONTEXT;
793 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
797 const egl_display_ptr dp = validate_display(dpy);
801 ContextRef _c(dp.get(), ctx);
803 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
805 egl_context_t * const c = get_context(ctx);
806 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
807 if (result == EGL_TRUE) {
813 EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
814 EGLSurface read, EGLContext ctx)
818 egl_display_ptr dp = validate_display(dpy);
819 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
821 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
822 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
823 // a valid but uninitialized display.
824 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
825 (draw != EGL_NO_SURFACE) ) {
826 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
829 // get a reference to the object passed in
830 ContextRef _c(dp.get(), ctx);
831 SurfaceRef _d(dp.get(), draw);
832 SurfaceRef _r(dp.get(), read);
834 // validate the context (if not EGL_NO_CONTEXT)
835 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
836 // EGL_NO_CONTEXT is valid
837 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
840 // these are the underlying implementation's object
841 EGLContext impl_ctx = EGL_NO_CONTEXT;
842 EGLSurface impl_draw = EGL_NO_SURFACE;
843 EGLSurface impl_read = EGL_NO_SURFACE;
845 // these are our objects structs passed in
846 egl_context_t * c = NULL;
847 egl_surface_t const * d = NULL;
848 egl_surface_t const * r = NULL;
850 // these are the current objects structs
851 egl_context_t * cur_c = get_context(getContext());
853 if (ctx != EGL_NO_CONTEXT) {
854 c = get_context(ctx);
855 impl_ctx = c->context;
857 // no context given, use the implementation of the current context
858 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
859 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
860 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
863 // no current context
864 // not an error, there is just no current context.
869 // retrieve the underlying implementation's draw EGLSurface
870 if (draw != EGL_NO_SURFACE) {
871 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
872 d = get_surface(draw);
873 impl_draw = d->surface;
876 // retrieve the underlying implementation's read EGLSurface
877 if (read != EGL_NO_SURFACE) {
878 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
879 r = get_surface(read);
880 impl_read = r->surface;
884 EGLBoolean result = dp->makeCurrent(c, cur_c,
886 impl_draw, impl_read, impl_ctx);
888 if (result == EGL_TRUE) {
890 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
891 egl_tls_t::setContext(ctx);
896 setGLHooksThreadSpecific(&gHooksNoContext);
897 egl_tls_t::setContext(EGL_NO_CONTEXT);
902 // Force return to current context for drivers that cannot handle errors
903 EGLBoolean restore_result = EGL_FALSE;
904 // get a reference to the old current objects
905 ContextRef _c2(dp.get(), cur_c);
906 SurfaceRef _d2(dp.get(), cur_c->draw);
907 SurfaceRef _r2(dp.get(), cur_c->read);
910 impl_ctx = c->context;
911 impl_draw = EGL_NO_SURFACE;
912 if (cur_c->draw != EGL_NO_SURFACE) {
913 d = get_surface(cur_c->draw);
914 impl_draw = d->surface;
916 impl_read = EGL_NO_SURFACE;
917 if (cur_c->read != EGL_NO_SURFACE) {
918 r = get_surface(cur_c->read);
919 impl_read = r->surface;
921 restore_result = dp->makeCurrent(c, cur_c,
922 cur_c->draw, cur_c->read, cur_c->context,
923 impl_draw, impl_read, impl_ctx);
924 if (restore_result == EGL_TRUE) {
929 ALOGE("Could not restore original EGL context");
932 // this will ALOGE the error
933 egl_connection_t* const cnx = &gEGLImpl;
934 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
940 EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
941 EGLint attribute, EGLint *value)
945 const egl_display_ptr dp = validate_display(dpy);
946 if (!dp) return EGL_FALSE;
948 ContextRef _c(dp.get(), ctx);
949 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
951 egl_context_t * const c = get_context(ctx);
952 return c->cnx->egl.eglQueryContext(
953 dp->disp.dpy, c->context, attribute, value);
957 EGLContext eglGetCurrentContext(void)
959 // could be called before eglInitialize(), but we wouldn't have a context
960 // then, and this function would correctly return EGL_NO_CONTEXT.
964 EGLContext ctx = getContext();
968 EGLSurface eglGetCurrentSurface(EGLint readdraw)
970 // could be called before eglInitialize(), but we wouldn't have a context
971 // then, and this function would correctly return EGL_NO_SURFACE.
975 EGLContext ctx = getContext();
977 egl_context_t const * const c = get_context(ctx);
978 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
980 case EGL_READ: return c->read;
981 case EGL_DRAW: return c->draw;
982 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
985 return EGL_NO_SURFACE;
988 EGLDisplay eglGetCurrentDisplay(void)
990 // could be called before eglInitialize(), but we wouldn't have a context
991 // then, and this function would correctly return EGL_NO_DISPLAY.
995 EGLContext ctx = getContext();
997 egl_context_t const * const c = get_context(ctx);
998 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1001 return EGL_NO_DISPLAY;
1004 EGLBoolean eglWaitGL(void)
1008 egl_connection_t* const cnx = &gEGLImpl;
1010 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1012 return cnx->egl.eglWaitGL();
1015 EGLBoolean eglWaitNative(EGLint engine)
1019 egl_connection_t* const cnx = &gEGLImpl;
1021 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1023 return cnx->egl.eglWaitNative(engine);
1026 EGLint eglGetError(void)
1028 EGLint err = EGL_SUCCESS;
1029 egl_connection_t* const cnx = &gEGLImpl;
1031 err = cnx->egl.eglGetError();
1033 if (err == EGL_SUCCESS) {
1034 err = egl_tls_t::getError();
1039 static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
1040 const char* procname) {
1041 const egl_connection_t* cnx = &gEGLImpl;
1044 proc = dlsym(cnx->libEgl, procname);
1045 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1047 proc = dlsym(cnx->libGles2, procname);
1048 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1050 proc = dlsym(cnx->libGles1, procname);
1051 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1056 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1058 // eglGetProcAddress() could be the very first function called
1059 // in which case we must make sure we've initialized ourselves, this
1060 // happens the first time egl_get_display() is called.
1064 if (egl_init_drivers() == EGL_FALSE) {
1065 setError(EGL_BAD_PARAMETER, NULL);
1069 if (FILTER_EXTENSIONS(procname)) {
1073 __eglMustCastToProperFunctionPointerType addr;
1074 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
1075 if (addr) return addr;
1077 addr = findBuiltinWrapper(procname);
1078 if (addr) return addr;
1080 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1081 pthread_mutex_lock(&sExtensionMapMutex);
1084 * Since eglGetProcAddress() is not associated to anything, it needs
1085 * to return a function pointer that "works" regardless of what
1086 * the current context is.
1088 * For this reason, we return a "forwarder", a small stub that takes
1089 * care of calling the function associated with the context
1092 * We first look for extensions we've already resolved, if we're seeing
1093 * this extension for the first time, we go through all our
1094 * implementations and call eglGetProcAddress() and record the
1095 * result in the appropriate implementation hooks and return the
1096 * address of the forwarder corresponding to that hook set.
1100 const std::string name(procname);
1102 auto& extentionMap = sGLExtentionMap;
1103 auto pos = extentionMap.find(name);
1104 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
1105 const int slot = sGLExtentionSlot;
1107 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1108 "no more slots for eglGetProcAddress(\"%s\")",
1111 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1114 egl_connection_t* const cnx = &gEGLImpl;
1115 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1116 // Extensions are independent of the bound context
1118 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1119 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
1120 cnx->egl.eglGetProcAddress(procname);
1121 if (addr) found = true;
1125 addr = gExtensionForwarders[slot];
1126 extentionMap[name] = addr;
1131 pthread_mutex_unlock(&sExtensionMapMutex);
1135 class FrameCompletionThread {
1138 static void queueSync(EGLSyncKHR sync) {
1139 static FrameCompletionThread thread;
1143 std::lock_guard<std::mutex> lock(thread.mMutex);
1144 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1147 thread.mQueue.push_back(sync);
1148 thread.mCondition.notify_one();
1149 thread.mFramesQueued++;
1150 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
1155 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1156 std::thread thread(&FrameCompletionThread::loop, this);
1160 #pragma clang diagnostic push
1161 #pragma clang diagnostic ignored "-Wmissing-noreturn"
1167 #pragma clang diagnostic pop
1173 std::unique_lock<std::mutex> lock(mMutex);
1174 while (mQueue.empty()) {
1175 mCondition.wait(lock);
1178 frameNum = mFramesCompleted;
1180 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1183 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1186 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1187 if (result == EGL_FALSE) {
1188 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1189 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1190 ALOGE("FrameCompletion: timeout waiting for fence");
1192 eglDestroySyncKHR(dpy, sync);
1195 std::lock_guard<std::mutex> lock(mMutex);
1198 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
1202 uint32_t mFramesQueued;
1203 uint32_t mFramesCompleted;
1204 std::deque<EGLSyncKHR> mQueue;
1205 std::condition_variable mCondition;
1209 EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1210 EGLint *rects, EGLint n_rects)
1215 const egl_display_ptr dp = validate_display(dpy);
1216 if (!dp) return EGL_FALSE;
1218 SurfaceRef _s(dp.get(), draw);
1220 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1222 egl_surface_t const * const s = get_surface(draw);
1224 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1225 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1226 if (sync != EGL_NO_SYNC_KHR) {
1227 FrameCompletionThread::queueSync(sync);
1231 if (CC_UNLIKELY(dp->finishOnSwap)) {
1233 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1235 // glReadPixels() ensures that the frame is complete
1236 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1237 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1242 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1245 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
1246 for (int r = 0; r < n_rects; ++r) {
1248 int x = rects[offset];
1249 int y = rects[offset + 1];
1250 int width = rects[offset + 2];
1251 int height = rects[offset + 3];
1252 android_native_rect_t androidRect;
1253 androidRect.left = x;
1254 androidRect.top = y + height;
1255 androidRect.right = x + width;
1256 androidRect.bottom = y;
1257 androidRects.push_back(androidRect);
1259 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
1261 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1262 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1265 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1269 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1271 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
1274 EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1275 NativePixmapType target)
1279 const egl_display_ptr dp = validate_display(dpy);
1280 if (!dp) return EGL_FALSE;
1282 SurfaceRef _s(dp.get(), surface);
1284 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1286 egl_surface_t const * const s = get_surface(surface);
1287 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
1290 const char* eglQueryString(EGLDisplay dpy, EGLint name)
1294 // Generate an error quietly when client extensions (as defined by
1295 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1296 // validate_display to generate the error as validate_display would log
1297 // the error, which can be misleading.
1299 // If we want to support EGL_EXT_client_extensions later, we can return
1300 // the client extension string here instead.
1301 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1302 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
1304 const egl_display_ptr dp = validate_display(dpy);
1305 if (!dp) return (const char *) NULL;
1309 return dp->getVendorString();
1311 return dp->getVersionString();
1312 case EGL_EXTENSIONS:
1313 return dp->getExtensionString();
1314 case EGL_CLIENT_APIS:
1315 return dp->getClientApiString();
1319 return setError(EGL_BAD_PARAMETER, (const char *)0);
1322 EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1326 const egl_display_ptr dp = validate_display(dpy);
1327 if (!dp) return (const char *) NULL;
1331 return dp->disp.queryString.vendor;
1333 return dp->disp.queryString.version;
1334 case EGL_EXTENSIONS:
1335 return dp->disp.queryString.extensions;
1336 case EGL_CLIENT_APIS:
1337 return dp->disp.queryString.clientApi;
1341 return setError(EGL_BAD_PARAMETER, (const char *)0);
1344 // ----------------------------------------------------------------------------
1346 // ----------------------------------------------------------------------------
1348 EGLBoolean eglSurfaceAttrib(
1349 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1353 const egl_display_ptr dp = validate_display(dpy);
1354 if (!dp) return EGL_FALSE;
1356 SurfaceRef _s(dp.get(), surface);
1358 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1360 egl_surface_t * const s = get_surface(surface);
1362 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
1363 if (!s->getNativeWindow()) {
1364 setError(EGL_BAD_SURFACE, EGL_FALSE);
1366 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1367 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1370 if (attribute == EGL_TIMESTAMPS_ANDROID) {
1371 if (!s->getNativeWindow()) {
1372 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1374 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1375 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1378 if (s->cnx->egl.eglSurfaceAttrib) {
1379 return s->cnx->egl.eglSurfaceAttrib(
1380 dp->disp.dpy, s->surface, attribute, value);
1382 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1385 EGLBoolean eglBindTexImage(
1386 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1390 const egl_display_ptr dp = validate_display(dpy);
1391 if (!dp) return EGL_FALSE;
1393 SurfaceRef _s(dp.get(), surface);
1395 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1397 egl_surface_t const * const s = get_surface(surface);
1398 if (s->cnx->egl.eglBindTexImage) {
1399 return s->cnx->egl.eglBindTexImage(
1400 dp->disp.dpy, s->surface, buffer);
1402 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1405 EGLBoolean eglReleaseTexImage(
1406 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1410 const egl_display_ptr dp = validate_display(dpy);
1411 if (!dp) return EGL_FALSE;
1413 SurfaceRef _s(dp.get(), surface);
1415 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1417 egl_surface_t const * const s = get_surface(surface);
1418 if (s->cnx->egl.eglReleaseTexImage) {
1419 return s->cnx->egl.eglReleaseTexImage(
1420 dp->disp.dpy, s->surface, buffer);
1422 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1425 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1429 const egl_display_ptr dp = validate_display(dpy);
1430 if (!dp) return EGL_FALSE;
1432 EGLBoolean res = EGL_TRUE;
1433 egl_connection_t* const cnx = &gEGLImpl;
1434 if (cnx->dso && cnx->egl.eglSwapInterval) {
1435 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
1442 // ----------------------------------------------------------------------------
1444 // ----------------------------------------------------------------------------
1446 EGLBoolean eglWaitClient(void)
1450 egl_connection_t* const cnx = &gEGLImpl;
1452 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1455 if (cnx->egl.eglWaitClient) {
1456 res = cnx->egl.eglWaitClient();
1458 res = cnx->egl.eglWaitGL();
1463 EGLBoolean eglBindAPI(EGLenum api)
1467 if (egl_init_drivers() == EGL_FALSE) {
1468 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
1471 // bind this API on all EGLs
1472 EGLBoolean res = EGL_TRUE;
1473 egl_connection_t* const cnx = &gEGLImpl;
1474 if (cnx->dso && cnx->egl.eglBindAPI) {
1475 res = cnx->egl.eglBindAPI(api);
1480 EGLenum eglQueryAPI(void)
1484 if (egl_init_drivers() == EGL_FALSE) {
1485 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
1488 egl_connection_t* const cnx = &gEGLImpl;
1489 if (cnx->dso && cnx->egl.eglQueryAPI) {
1490 return cnx->egl.eglQueryAPI();
1493 // or, it can only be OpenGL ES
1494 return EGL_OPENGL_ES_API;
1497 EGLBoolean eglReleaseThread(void)
1501 egl_connection_t* const cnx = &gEGLImpl;
1502 if (cnx->dso && cnx->egl.eglReleaseThread) {
1503 cnx->egl.eglReleaseThread();
1506 // If there is context bound to the thread, release it
1507 egl_display_t::loseCurrent(get_context(getContext()));
1509 egl_tls_t::clearTLS();
1513 EGLSurface eglCreatePbufferFromClientBuffer(
1514 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1515 EGLConfig config, const EGLint *attrib_list)
1519 egl_connection_t* cnx = NULL;
1520 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1521 if (!dp) return EGL_FALSE;
1522 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1523 return cnx->egl.eglCreatePbufferFromClientBuffer(
1524 dp->disp.dpy, buftype, buffer, config, attrib_list);
1526 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1529 // ----------------------------------------------------------------------------
1530 // EGL_EGLEXT_VERSION 3
1531 // ----------------------------------------------------------------------------
1533 EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1534 const EGLint *attrib_list)
1538 const egl_display_ptr dp = validate_display(dpy);
1539 if (!dp) return EGL_FALSE;
1541 SurfaceRef _s(dp.get(), surface);
1543 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1545 egl_surface_t const * const s = get_surface(surface);
1546 if (s->cnx->egl.eglLockSurfaceKHR) {
1547 return s->cnx->egl.eglLockSurfaceKHR(
1548 dp->disp.dpy, s->surface, attrib_list);
1550 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
1553 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1557 const egl_display_ptr dp = validate_display(dpy);
1558 if (!dp) return EGL_FALSE;
1560 SurfaceRef _s(dp.get(), surface);
1562 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1564 egl_surface_t const * const s = get_surface(surface);
1565 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1566 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
1568 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
1571 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1572 EGLClientBuffer buffer, const EGLint *attrib_list)
1576 const egl_display_ptr dp = validate_display(dpy);
1577 if (!dp) return EGL_NO_IMAGE_KHR;
1579 ContextRef _c(dp.get(), ctx);
1580 egl_context_t * const c = _c.get();
1582 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1583 egl_connection_t* const cnx = &gEGLImpl;
1584 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1585 result = cnx->egl.eglCreateImageKHR(
1587 c ? c->context : EGL_NO_CONTEXT,
1588 target, buffer, attrib_list);
1593 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1597 const egl_display_ptr dp = validate_display(dpy);
1598 if (!dp) return EGL_FALSE;
1600 EGLBoolean result = EGL_FALSE;
1601 egl_connection_t* const cnx = &gEGLImpl;
1602 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
1603 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
1608 // ----------------------------------------------------------------------------
1609 // EGL_EGLEXT_VERSION 5
1610 // ----------------------------------------------------------------------------
1613 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1617 const egl_display_ptr dp = validate_display(dpy);
1618 if (!dp) return EGL_NO_SYNC_KHR;
1620 EGLSyncKHR result = EGL_NO_SYNC_KHR;
1621 egl_connection_t* const cnx = &gEGLImpl;
1622 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1623 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
1628 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1632 const egl_display_ptr dp = validate_display(dpy);
1633 if (!dp) return EGL_FALSE;
1635 EGLBoolean result = EGL_FALSE;
1636 egl_connection_t* const cnx = &gEGLImpl;
1637 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1638 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
1643 EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1646 const egl_display_ptr dp = validate_display(dpy);
1647 if (!dp) return EGL_FALSE;
1649 EGLBoolean result = EGL_FALSE;
1650 egl_connection_t* const cnx = &gEGLImpl;
1651 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1652 result = cnx->egl.eglSignalSyncKHR(
1653 dp->disp.dpy, sync, mode);
1658 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1659 EGLint flags, EGLTimeKHR timeout)
1663 const egl_display_ptr dp = validate_display(dpy);
1664 if (!dp) return EGL_FALSE;
1666 EGLint result = EGL_FALSE;
1667 egl_connection_t* const cnx = &gEGLImpl;
1668 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1669 result = cnx->egl.eglClientWaitSyncKHR(
1670 dp->disp.dpy, sync, flags, timeout);
1675 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1676 EGLint attribute, EGLint *value)
1680 const egl_display_ptr dp = validate_display(dpy);
1681 if (!dp) return EGL_FALSE;
1683 EGLBoolean result = EGL_FALSE;
1684 egl_connection_t* const cnx = &gEGLImpl;
1685 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1686 result = cnx->egl.eglGetSyncAttribKHR(
1687 dp->disp.dpy, sync, attribute, value);
1692 EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1696 const egl_display_ptr dp = validate_display(dpy);
1697 if (!dp) return EGL_NO_STREAM_KHR;
1699 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1700 egl_connection_t* const cnx = &gEGLImpl;
1701 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1702 result = cnx->egl.eglCreateStreamKHR(
1703 dp->disp.dpy, attrib_list);
1708 EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1712 const egl_display_ptr dp = validate_display(dpy);
1713 if (!dp) return EGL_FALSE;
1715 EGLBoolean result = EGL_FALSE;
1716 egl_connection_t* const cnx = &gEGLImpl;
1717 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1718 result = cnx->egl.eglDestroyStreamKHR(
1719 dp->disp.dpy, stream);
1724 EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1725 EGLenum attribute, EGLint value)
1729 const egl_display_ptr dp = validate_display(dpy);
1730 if (!dp) return EGL_FALSE;
1732 EGLBoolean result = EGL_FALSE;
1733 egl_connection_t* const cnx = &gEGLImpl;
1734 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1735 result = cnx->egl.eglStreamAttribKHR(
1736 dp->disp.dpy, stream, attribute, value);
1741 EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1742 EGLenum attribute, EGLint *value)
1746 const egl_display_ptr dp = validate_display(dpy);
1747 if (!dp) return EGL_FALSE;
1749 EGLBoolean result = EGL_FALSE;
1750 egl_connection_t* const cnx = &gEGLImpl;
1751 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1752 result = cnx->egl.eglQueryStreamKHR(
1753 dp->disp.dpy, stream, attribute, value);
1758 EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1759 EGLenum attribute, EGLuint64KHR *value)
1763 const egl_display_ptr dp = validate_display(dpy);
1764 if (!dp) return EGL_FALSE;
1766 EGLBoolean result = EGL_FALSE;
1767 egl_connection_t* const cnx = &gEGLImpl;
1768 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1769 result = cnx->egl.eglQueryStreamu64KHR(
1770 dp->disp.dpy, stream, attribute, value);
1775 EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1776 EGLenum attribute, EGLTimeKHR *value)
1780 const egl_display_ptr dp = validate_display(dpy);
1781 if (!dp) return EGL_FALSE;
1783 EGLBoolean result = EGL_FALSE;
1784 egl_connection_t* const cnx = &gEGLImpl;
1785 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1786 result = cnx->egl.eglQueryStreamTimeKHR(
1787 dp->disp.dpy, stream, attribute, value);
1792 EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1793 EGLStreamKHR stream, const EGLint *attrib_list)
1797 egl_display_ptr dp = validate_display(dpy);
1798 if (!dp) return EGL_NO_SURFACE;
1800 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
1801 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
1802 // TODO: Probably need to update EGL_KHR_stream_producer_eglsurface to
1803 // indicate support for EGL_GL_COLORSPACE_KHR.
1804 // now select a corresponding sRGB format if needed
1805 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
1806 ALOGE("error invalid colorspace: %d", colorSpace);
1807 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1810 egl_connection_t* const cnx = &gEGLImpl;
1811 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1812 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1813 dp->disp.dpy, config, stream, attrib_list);
1814 if (surface != EGL_NO_SURFACE) {
1815 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
1819 return EGL_NO_SURFACE;
1822 EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1823 EGLStreamKHR stream)
1827 const egl_display_ptr dp = validate_display(dpy);
1828 if (!dp) return EGL_FALSE;
1830 EGLBoolean result = EGL_FALSE;
1831 egl_connection_t* const cnx = &gEGLImpl;
1832 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1833 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1834 dp->disp.dpy, stream);
1839 EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1840 EGLStreamKHR stream)
1844 const egl_display_ptr dp = validate_display(dpy);
1845 if (!dp) return EGL_FALSE;
1847 EGLBoolean result = EGL_FALSE;
1848 egl_connection_t* const cnx = &gEGLImpl;
1849 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1850 result = cnx->egl.eglStreamConsumerAcquireKHR(
1851 dp->disp.dpy, stream);
1856 EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1857 EGLStreamKHR stream)
1861 const egl_display_ptr dp = validate_display(dpy);
1862 if (!dp) return EGL_FALSE;
1864 EGLBoolean result = EGL_FALSE;
1865 egl_connection_t* const cnx = &gEGLImpl;
1866 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1867 result = cnx->egl.eglStreamConsumerReleaseKHR(
1868 dp->disp.dpy, stream);
1873 EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1874 EGLDisplay dpy, EGLStreamKHR stream)
1878 const egl_display_ptr dp = validate_display(dpy);
1879 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1881 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1882 egl_connection_t* const cnx = &gEGLImpl;
1883 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1884 result = cnx->egl.eglGetStreamFileDescriptorKHR(
1885 dp->disp.dpy, stream);
1890 EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1891 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1895 const egl_display_ptr dp = validate_display(dpy);
1896 if (!dp) return EGL_NO_STREAM_KHR;
1898 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1899 egl_connection_t* const cnx = &gEGLImpl;
1900 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1901 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1902 dp->disp.dpy, file_descriptor);
1907 // ----------------------------------------------------------------------------
1908 // EGL_EGLEXT_VERSION 15
1909 // ----------------------------------------------------------------------------
1911 EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1913 const egl_display_ptr dp = validate_display(dpy);
1914 if (!dp) return EGL_FALSE;
1915 EGLint result = EGL_FALSE;
1916 egl_connection_t* const cnx = &gEGLImpl;
1917 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1918 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1923 // ----------------------------------------------------------------------------
1924 // ANDROID extensions
1925 // ----------------------------------------------------------------------------
1927 EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1931 const egl_display_ptr dp = validate_display(dpy);
1932 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1934 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1935 egl_connection_t* const cnx = &gEGLImpl;
1936 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1937 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1942 EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1943 EGLnsecsANDROID time)
1947 const egl_display_ptr dp = validate_display(dpy);
1952 SurfaceRef _s(dp.get(), surface);
1954 setError(EGL_BAD_SURFACE, EGL_FALSE);
1958 egl_surface_t const * const s = get_surface(surface);
1959 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
1964 EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
1966 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1967 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
1970 // ----------------------------------------------------------------------------
1971 // NVIDIA extensions
1972 // ----------------------------------------------------------------------------
1973 EGLuint64NV eglGetSystemTimeFrequencyNV()
1977 if (egl_init_drivers() == EGL_FALSE) {
1978 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
1981 EGLuint64NV ret = 0;
1982 egl_connection_t* const cnx = &gEGLImpl;
1984 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1985 return cnx->egl.eglGetSystemTimeFrequencyNV();
1988 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
1991 EGLuint64NV eglGetSystemTimeNV()
1995 if (egl_init_drivers() == EGL_FALSE) {
1996 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
1999 EGLuint64NV ret = 0;
2000 egl_connection_t* const cnx = &gEGLImpl;
2002 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2003 return cnx->egl.eglGetSystemTimeNV();
2006 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
2009 // ----------------------------------------------------------------------------
2010 // Partial update extension
2011 // ----------------------------------------------------------------------------
2012 EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2013 EGLint *rects, EGLint n_rects)
2017 const egl_display_ptr dp = validate_display(dpy);
2019 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2023 SurfaceRef _s(dp.get(), surface);
2025 setError(EGL_BAD_SURFACE, EGL_FALSE);
2029 egl_surface_t const * const s = get_surface(surface);
2030 if (s->cnx->egl.eglSetDamageRegionKHR) {
2031 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2038 EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2039 EGLuint64KHR *frameId) {
2042 const egl_display_ptr dp = validate_display(dpy);
2044 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2047 SurfaceRef _s(dp.get(), surface);
2049 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2052 egl_surface_t const * const s = get_surface(surface);
2054 if (!s->getNativeWindow()) {
2055 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2058 uint64_t nextFrameId = 0;
2059 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
2062 // This should not happen. Return an error that is not in the spec
2063 // so it's obvious something is very wrong.
2064 ALOGE("eglGetNextFrameId: Unexpected error.");
2065 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
2068 *frameId = nextFrameId;
2072 EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2073 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2077 const egl_display_ptr dp = validate_display(dpy);
2079 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2082 SurfaceRef _s(dp.get(), surface);
2084 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2087 egl_surface_t const * const s = get_surface(surface);
2089 if (!s->getNativeWindow()) {
2090 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2093 nsecs_t* compositeDeadline = nullptr;
2094 nsecs_t* compositeInterval = nullptr;
2095 nsecs_t* compositeToPresentLatency = nullptr;
2097 for (int i = 0; i < numTimestamps; i++) {
2099 case EGL_COMPOSITE_DEADLINE_ANDROID:
2100 compositeDeadline = &values[i];
2102 case EGL_COMPOSITE_INTERVAL_ANDROID:
2103 compositeInterval = &values[i];
2105 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2106 compositeToPresentLatency = &values[i];
2109 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2113 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
2114 compositeDeadline, compositeInterval, compositeToPresentLatency);
2120 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2122 // This should not happen. Return an error that is not in the spec
2123 // so it's obvious something is very wrong.
2124 ALOGE("eglGetCompositorTiming: Unexpected error.");
2125 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
2129 EGLBoolean eglGetCompositorTimingSupportedANDROID(
2130 EGLDisplay dpy, EGLSurface surface, EGLint name)
2134 const egl_display_ptr dp = validate_display(dpy);
2136 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2139 SurfaceRef _s(dp.get(), surface);
2141 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2144 egl_surface_t const * const s = get_surface(surface);
2146 ANativeWindow* window = s->getNativeWindow();
2148 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2152 case EGL_COMPOSITE_DEADLINE_ANDROID:
2153 case EGL_COMPOSITE_INTERVAL_ANDROID:
2154 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2161 EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
2162 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
2163 EGLnsecsANDROID *values)
2167 const egl_display_ptr dp = validate_display(dpy);
2169 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2172 SurfaceRef _s(dp.get(), surface);
2174 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2177 egl_surface_t const * const s = get_surface(surface);
2179 if (!s->getNativeWindow()) {
2180 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2183 nsecs_t* requestedPresentTime = nullptr;
2184 nsecs_t* acquireTime = nullptr;
2185 nsecs_t* latchTime = nullptr;
2186 nsecs_t* firstRefreshStartTime = nullptr;
2187 nsecs_t* gpuCompositionDoneTime = nullptr;
2188 nsecs_t* lastRefreshStartTime = nullptr;
2189 nsecs_t* displayPresentTime = nullptr;
2190 nsecs_t* dequeueReadyTime = nullptr;
2191 nsecs_t* releaseTime = nullptr;
2193 for (int i = 0; i < numTimestamps; i++) {
2194 switch (timestamps[i]) {
2195 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2196 requestedPresentTime = &values[i];
2198 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2199 acquireTime = &values[i];
2201 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2202 latchTime = &values[i];
2204 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2205 firstRefreshStartTime = &values[i];
2207 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2208 lastRefreshStartTime = &values[i];
2210 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2211 gpuCompositionDoneTime = &values[i];
2213 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2214 displayPresentTime = &values[i];
2216 case EGL_DEQUEUE_READY_TIME_ANDROID:
2217 dequeueReadyTime = &values[i];
2219 case EGL_READS_DONE_TIME_ANDROID:
2220 releaseTime = &values[i];
2223 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2227 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
2228 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
2229 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
2230 dequeueReadyTime, releaseTime);
2236 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
2238 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2240 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2242 // This should not happen. Return an error that is not in the spec
2243 // so it's obvious something is very wrong.
2244 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2245 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
2249 EGLBoolean eglGetFrameTimestampSupportedANDROID(
2250 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
2254 const egl_display_ptr dp = validate_display(dpy);
2256 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2259 SurfaceRef _s(dp.get(), surface);
2261 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2264 egl_surface_t const * const s = get_surface(surface);
2266 ANativeWindow* window = s->getNativeWindow();
2268 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2271 switch (timestamp) {
2272 case EGL_COMPOSITE_DEADLINE_ANDROID:
2273 case EGL_COMPOSITE_INTERVAL_ANDROID:
2274 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2275 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2276 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2277 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2278 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2279 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2280 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2281 case EGL_DEQUEUE_READY_TIME_ANDROID:
2282 case EGL_READS_DONE_TIME_ANDROID:
2284 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2286 window->query(window,
2287 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2288 return value == 0 ? EGL_FALSE : EGL_TRUE;