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 "
92 char const * const gExtensionString =
93 "EGL_KHR_image " // mandatory
94 "EGL_KHR_image_base " // mandatory
95 "EGL_KHR_image_pixmap "
96 "EGL_KHR_lock_surface "
97 "EGL_KHR_gl_colorspace "
98 "EGL_KHR_gl_texture_2D_image "
99 "EGL_KHR_gl_texture_3D_image "
100 "EGL_KHR_gl_texture_cubemap_image "
101 "EGL_KHR_gl_renderbuffer_image "
102 "EGL_KHR_reusable_sync "
103 "EGL_KHR_fence_sync "
104 "EGL_KHR_create_context "
105 "EGL_KHR_config_attribs "
106 "EGL_KHR_surfaceless_context "
108 "EGL_KHR_stream_fifo "
109 "EGL_KHR_stream_producer_eglsurface "
110 "EGL_KHR_stream_consumer_gltexture "
111 "EGL_KHR_stream_cross_process_fd "
112 "EGL_EXT_create_context_robustness "
113 "EGL_NV_system_time "
114 "EGL_ANDROID_image_native_buffer " // mandatory
115 "EGL_KHR_wait_sync " // strongly recommended
116 "EGL_ANDROID_recordable " // mandatory
117 "EGL_KHR_partial_update " // strongly recommended
118 "EGL_EXT_pixel_format_float "
119 "EGL_EXT_buffer_age " // strongly recommended with partial_update
120 "EGL_KHR_create_context_no_error "
121 "EGL_KHR_mutable_render_buffer "
122 "EGL_EXT_yuv_surface "
123 "EGL_EXT_protected_content "
124 "EGL_IMG_context_priority "
125 "EGL_KHR_no_config_context "
129 // extensions not exposed to applications but used by the ANDROID system
130 // "EGL_ANDROID_blob_cache " // strongly recommended
131 // "EGL_IMG_hibernate_process " // optional
132 // "EGL_ANDROID_native_fence_sync " // strongly recommended
133 // "EGL_ANDROID_framebuffer_target " // mandatory for HWC 1.1
134 // "EGL_ANDROID_image_crop " // optional
137 * EGL Extensions entry-points exposed to 3rd party applications
138 * (keep in sync with gExtensionString above)
141 static const extention_map_t sExtensionMap[] = {
142 // EGL_KHR_lock_surface
143 { "eglLockSurfaceKHR",
144 (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
145 { "eglUnlockSurfaceKHR",
146 (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
148 // EGL_KHR_image, EGL_KHR_image_base
149 { "eglCreateImageKHR",
150 (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
151 { "eglDestroyImageKHR",
152 (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
154 // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
155 { "eglCreateSyncKHR",
156 (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
157 { "eglDestroySyncKHR",
158 (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
159 { "eglClientWaitSyncKHR",
160 (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
161 { "eglSignalSyncKHR",
162 (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
163 { "eglGetSyncAttribKHR",
164 (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
166 // EGL_NV_system_time
167 { "eglGetSystemTimeFrequencyNV",
168 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
169 { "eglGetSystemTimeNV",
170 (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
174 (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
176 // EGL_ANDROID_presentation_time
177 { "eglPresentationTimeANDROID",
178 (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
180 // EGL_KHR_swap_buffers_with_damage
181 { "eglSwapBuffersWithDamageKHR",
182 (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
184 // EGL_ANDROID_get_native_client_buffer
185 { "eglGetNativeClientBufferANDROID",
186 (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
188 // EGL_KHR_partial_update
189 { "eglSetDamageRegionKHR",
190 (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
192 { "eglCreateStreamKHR",
193 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
194 { "eglDestroyStreamKHR",
195 (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
196 { "eglStreamAttribKHR",
197 (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
198 { "eglQueryStreamKHR",
199 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
200 { "eglQueryStreamu64KHR",
201 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
202 { "eglQueryStreamTimeKHR",
203 (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
204 { "eglCreateStreamProducerSurfaceKHR",
205 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
206 { "eglStreamConsumerGLTextureExternalKHR",
207 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
208 { "eglStreamConsumerAcquireKHR",
209 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
210 { "eglStreamConsumerReleaseKHR",
211 (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
212 { "eglGetStreamFileDescriptorKHR",
213 (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
214 { "eglCreateStreamFromFileDescriptorKHR",
215 (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
217 // EGL_ANDROID_get_frame_timestamps
218 { "eglGetNextFrameIdANDROID",
219 (__eglMustCastToProperFunctionPointerType)&eglGetNextFrameIdANDROID },
220 { "eglGetCompositorTimingANDROID",
221 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingANDROID },
222 { "eglGetCompositorTimingSupportedANDROID",
223 (__eglMustCastToProperFunctionPointerType)&eglGetCompositorTimingSupportedANDROID },
224 { "eglGetFrameTimestampsANDROID",
225 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampsANDROID },
226 { "eglGetFrameTimestampSupportedANDROID",
227 (__eglMustCastToProperFunctionPointerType)&eglGetFrameTimestampSupportedANDROID },
229 // EGL_ANDROID_native_fence_sync
230 { "eglDupNativeFenceFDANDROID",
231 (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
235 * These extensions entry-points should not be exposed to applications.
236 * They're used internally by the Android EGL layer.
238 #define FILTER_EXTENSIONS(procname) \
239 (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") || \
240 !strcmp((procname), "eglHibernateProcessIMG") || \
241 !strcmp((procname), "eglAwakenProcessIMG"))
245 // accesses protected by sExtensionMapMutex
246 static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
248 static int sGLExtentionSlot = 0;
249 static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
251 static void(*findProcAddress(const char* name,
252 const extention_map_t* map, size_t n))() {
253 for (uint32_t i=0 ; i<n ; i++) {
254 if (!strcmp(name, map[i].name)) {
255 return map[i].address;
261 // ----------------------------------------------------------------------------
263 extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
264 extern EGLBoolean egl_init_drivers();
265 extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
266 extern gl_hooks_t gHooksTrace;
268 } // namespace android;
271 // ----------------------------------------------------------------------------
273 static inline void clearError() { egl_tls_t::clearError(); }
274 static inline EGLContext getContext() { return egl_tls_t::getContext(); }
276 // ----------------------------------------------------------------------------
278 EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
283 uintptr_t index = reinterpret_cast<uintptr_t>(display);
284 if (index >= NUM_DISPLAYS) {
285 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
288 if (egl_init_drivers() == EGL_FALSE) {
289 return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
292 EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
304 egl_display_ptr dp = get_display(dpy);
305 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
307 EGLBoolean res = dp->initialize(major, minor);
312 EGLBoolean eglTerminate(EGLDisplay dpy)
314 // NOTE: don't unload the drivers b/c some APIs can be called
315 // after eglTerminate() has been called. eglTerminate() only
316 // terminates an EGLDisplay, not a EGL itself.
320 egl_display_ptr dp = get_display(dpy);
321 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
323 EGLBoolean res = dp->terminate();
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 EGLBoolean eglGetConfigs( EGLDisplay dpy,
334 EGLint config_size, EGLint *num_config)
338 const egl_display_ptr dp = validate_display(dpy);
339 if (!dp) return EGL_FALSE;
342 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
345 EGLBoolean res = EGL_FALSE;
348 egl_connection_t* const cnx = &gEGLImpl;
350 res = cnx->egl.eglGetConfigs(
351 dp->disp.dpy, configs, config_size, num_config);
357 EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
358 EGLConfig *configs, EGLint config_size,
363 const egl_display_ptr dp = validate_display(dpy);
364 if (!dp) return EGL_FALSE;
367 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
370 EGLBoolean res = EGL_FALSE;
373 egl_connection_t* const cnx = &gEGLImpl;
376 char value[PROPERTY_VALUE_MAX];
377 property_get("debug.egl.force_msaa", value, "false");
379 if (!strcmp(value, "true")) {
380 size_t attribCount = 0;
381 EGLint attrib = attrib_list[0];
383 // Only enable MSAA if the context is OpenGL ES 2.0 and
384 // if no caveat is requested
385 const EGLint *attribRendererable = NULL;
386 const EGLint *attribCaveat = NULL;
388 // Count the number of attributes and look for
389 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
390 while (attrib != EGL_NONE) {
391 attrib = attrib_list[attribCount];
393 case EGL_RENDERABLE_TYPE:
394 attribRendererable = &attrib_list[attribCount];
396 case EGL_CONFIG_CAVEAT:
397 attribCaveat = &attrib_list[attribCount];
405 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
406 (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
408 // Insert 2 extra attributes to force-enable MSAA 4x
409 EGLint aaAttribs[attribCount + 4];
410 aaAttribs[0] = EGL_SAMPLE_BUFFERS;
412 aaAttribs[2] = EGL_SAMPLES;
415 memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
418 EGLBoolean resAA = cnx->egl.eglChooseConfig(
419 dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
421 if (resAA == EGL_TRUE && numConfigAA > 0) {
422 ALOGD("Enabling MSAA 4x");
423 *num_config = numConfigAA;
430 res = cnx->egl.eglChooseConfig(
431 dp->disp.dpy, attrib_list, configs, config_size, num_config);
436 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
437 EGLint attribute, EGLint *value)
441 egl_connection_t* cnx = NULL;
442 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
443 if (!dp) return EGL_FALSE;
445 return cnx->egl.eglGetConfigAttrib(
446 dp->disp.dpy, config, attribute, value);
449 // ----------------------------------------------------------------------------
451 // ----------------------------------------------------------------------------
453 // Turn linear formats into corresponding sRGB formats when colorspace is
454 // EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
455 // formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
456 // the modification isn't possible, the original dataSpace is returned.
457 static android_dataspace modifyBufferDataspace(android_dataspace dataSpace,
459 if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
460 return HAL_DATASPACE_SRGB_LINEAR;
461 } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
462 return HAL_DATASPACE_SRGB;
463 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT) {
464 return HAL_DATASPACE_DISPLAY_P3;
465 } else if (colorspace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT) {
466 return HAL_DATASPACE_DISPLAY_P3_LINEAR;
467 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_EXT) {
468 return HAL_DATASPACE_V0_SCRGB;
469 } else if (colorspace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT) {
470 return HAL_DATASPACE_V0_SCRGB_LINEAR;
475 // Return true if we stripped any EGL_GL_COLORSPACE_KHR attributes.
476 static EGLBoolean stripColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
478 std::vector<EGLint>& stripped_attrib_list) {
479 std::vector<EGLint> allowedColorSpaces;
481 case HAL_PIXEL_FORMAT_RGBA_8888:
482 case HAL_PIXEL_FORMAT_RGB_565:
483 // driver okay with linear & sRGB for 8888, but can't handle
484 // Display-P3 or other spaces.
485 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_SRGB_KHR);
486 allowedColorSpaces.push_back(EGL_GL_COLORSPACE_LINEAR_KHR);
489 case HAL_PIXEL_FORMAT_RGBA_FP16:
490 case HAL_PIXEL_FORMAT_RGBA_1010102:
492 // driver does not want to see colorspace attributes for 1010102 or fp16.
493 // Future: if driver supports XXXX extension, we can pass down that colorspace
497 bool stripped = false;
498 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
499 for (const EGLint* attr = attrib_list; attr[0] != EGL_NONE; attr += 2) {
500 if (attr[0] == EGL_GL_COLORSPACE_KHR) {
501 EGLint colorSpace = attr[1];
503 // Verify that color space is allowed
504 for (auto it : allowedColorSpaces) {
505 if (colorSpace == it) {
512 stripped_attrib_list.push_back(attr[0]);
513 stripped_attrib_list.push_back(attr[1]);
516 stripped_attrib_list.push_back(attr[0]);
517 stripped_attrib_list.push_back(attr[1]);
522 stripped_attrib_list.push_back(EGL_NONE);
523 stripped_attrib_list.push_back(EGL_NONE);
528 static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, NativeWindowType window,
529 const EGLint* attrib_list, EGLint& colorSpace,
530 android_dataspace& dataSpace) {
531 colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
532 dataSpace = HAL_DATASPACE_UNKNOWN;
534 if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
535 for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
536 if (*attr == EGL_GL_COLORSPACE_KHR) {
537 colorSpace = attr[1];
540 // Verify that color space is allowed
541 if (colorSpace == EGL_GL_COLORSPACE_SRGB_KHR ||
542 colorSpace == EGL_GL_COLORSPACE_LINEAR_KHR) {
543 // SRGB and LINEAR are always supported when EGL_KHR_gl_colorspace
544 // is available, so no need to verify.
547 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_linear &&
548 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_linear")) {
550 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_pq &&
551 dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_pq")) {
553 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_EXT &&
554 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb")) {
556 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT &&
557 dp->haveExtension("EGL_EXT_gl_colorspace_scrgb_linear")) {
559 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT &&
560 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3_linear")) {
562 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT &&
563 dp->haveExtension("EGL_EXT_gl_colorspace_display_p3")) {
569 if (verify && window) {
570 bool wide_color_support = true;
571 // Ordinarily we'd put a call to native_window_get_wide_color_support
572 // at the beginning of the function so that we'll have the
573 // result when needed elsewhere in the function.
574 // However, because eglCreateWindowSurface is called by SurfaceFlinger and
575 // SurfaceFlinger is required to answer the call below we would
576 // end up in a deadlock situation. By moving the call to only happen
577 // if the application has specifically asked for wide-color we avoid
578 // the deadlock with SurfaceFlinger since it will not ask for a
579 // wide-color surface.
580 int err = native_window_get_wide_color_support(window, &wide_color_support);
583 ALOGE("getColorSpaceAttribute: invalid window (win=%p) "
584 "failed (%#x) (already connected to another API?)",
588 if (!wide_color_support) {
589 // Application has asked for a wide-color colorspace but
590 // wide-color support isn't available on the display the window is on.
594 // Only change the dataSpace from default if the application
595 // has explicitly set the color space with a EGL_GL_COLORSPACE_KHR attribute.
596 dataSpace = modifyBufferDataspace(dataSpace, colorSpace);
603 static EGLBoolean getColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
604 EGLint& colorSpace, android_dataspace& dataSpace) {
605 return getColorSpaceAttribute(dp, NULL, attrib_list, colorSpace, dataSpace);
608 void getNativePixelFormat(EGLDisplay dpy, egl_connection_t* cnx, EGLConfig config, EGLint& format) {
609 // Set the native window's buffers format to match what this config requests.
610 // Whether to use sRGB gamma is not part of the EGLconfig, but is part
611 // of our native format. So if sRGB gamma is requested, we have to
612 // modify the EGLconfig's format before setting the native window's
615 EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
616 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
621 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_RED_SIZE, &r);
622 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_GREEN_SIZE, &g);
623 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_BLUE_SIZE, &b);
624 cnx->egl.eglGetConfigAttrib(dpy, config, EGL_ALPHA_SIZE, &a);
625 EGLint colorDepth = r + g + b;
627 // Today, the driver only understands sRGB and linear on 888X
628 // formats. Strip other colorspaces from the attribute list and
629 // only use them to set the dataspace via
630 // native_window_set_buffers_dataspace
631 // if pixel format is RGBX 8888
632 // TBD: Can test for future extensions that indicate that driver
633 // handles requested color space and we can let it through.
634 // allow SRGB and LINEAR. All others need to be stripped.
636 // TBD: Can we assume these are supported if 8888 is?
637 // else if FP16 or 1010102
638 // strip colorspace from attribs.
641 if (colorDepth <= 16) {
642 format = HAL_PIXEL_FORMAT_RGB_565;
644 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
645 if (colorDepth > 24) {
646 format = HAL_PIXEL_FORMAT_RGBA_1010102;
648 format = HAL_PIXEL_FORMAT_RGBX_8888;
651 format = HAL_PIXEL_FORMAT_RGBA_FP16;
655 if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
656 if (colorDepth > 24) {
657 format = HAL_PIXEL_FORMAT_RGBA_1010102;
658 } else if (!cnx->egl.eglGetConfigAttrib(dpy, config, EGL_NATIVE_VISUAL_ID, &format)) {
659 ALOGE("eglGetConfigAttrib(EGL_NATIVE_VISUAL_ID) failed: %#x", eglGetError());
660 format = HAL_PIXEL_FORMAT_RGBA_8888;
663 format = HAL_PIXEL_FORMAT_RGBA_FP16;
668 EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config,
669 NativeWindowType window,
670 const EGLint *attrib_list)
674 egl_connection_t* cnx = NULL;
675 egl_display_ptr dp = validate_display_connection(dpy, cnx);
677 EGLDisplay iDpy = dp->disp.dpy;
680 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
684 window->query(window, NATIVE_WINDOW_IS_VALID, &value);
686 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
689 int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
691 ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
692 "failed (%#x) (already connected to another API?)",
694 return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
698 getNativePixelFormat(iDpy, cnx, config, format);
700 // now select correct colorspace and dataspace based on user's attribute list
702 android_dataspace dataSpace;
703 if (!getColorSpaceAttribute(dp, window, attrib_list, colorSpace, dataSpace)) {
704 ALOGE("error invalid colorspace: %d", colorSpace);
705 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
708 std::vector<EGLint> strippedAttribList;
709 if (stripColorSpaceAttribute(dp, attrib_list, format, strippedAttribList)) {
710 // Had to modify the attribute list due to use of color space.
711 // Use modified list from here on.
712 attrib_list = strippedAttribList.data();
716 int err = native_window_set_buffers_format(window, format);
718 ALOGE("error setting native window pixel format: %s (%d)",
719 strerror(-err), err);
720 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
721 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
725 if (dataSpace != 0) {
726 int err = native_window_set_buffers_data_space(window, dataSpace);
728 ALOGE("error setting native window pixel dataSpace: %s (%d)",
729 strerror(-err), err);
730 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
731 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
735 // the EGL spec requires that a new EGLSurface default to swap interval
736 // 1, so explicitly set that on the window here.
737 ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
738 anw->setSwapInterval(anw, 1);
740 EGLSurface surface = cnx->egl.eglCreateWindowSurface(
741 iDpy, config, window, attrib_list);
742 if (surface != EGL_NO_SURFACE) {
744 new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
748 // EGLSurface creation failed
749 native_window_set_buffers_format(window, 0);
750 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
752 return EGL_NO_SURFACE;
755 EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config,
756 NativePixmapType pixmap,
757 const EGLint *attrib_list)
761 egl_connection_t* cnx = NULL;
762 egl_display_ptr dp = validate_display_connection(dpy, cnx);
764 android_dataspace dataSpace;
766 // now select a corresponding sRGB format if needed
767 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
768 ALOGE("error invalid colorspace: %d", colorSpace);
769 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
772 EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
773 dp->disp.dpy, config, pixmap, attrib_list);
774 if (surface != EGL_NO_SURFACE) {
775 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
779 return EGL_NO_SURFACE;
782 EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
783 const EGLint *attrib_list)
787 egl_connection_t* cnx = NULL;
788 egl_display_ptr dp = validate_display_connection(dpy, cnx);
790 EGLDisplay iDpy = dp->disp.dpy;
792 getNativePixelFormat(iDpy, cnx, config, format);
794 // now select correct colorspace and dataspace based on user's attribute list
796 android_dataspace dataSpace;
797 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
798 ALOGE("error invalid colorspace: %d", colorSpace);
799 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
802 // Pbuffers are not displayed so we don't need to store the
803 // colorspace. We do need to filter out color spaces the
804 // driver doesn't know how to process.
805 std::vector<EGLint> strippedAttribList;
806 if (stripColorSpaceAttribute(dp, attrib_list, format, strippedAttribList)) {
807 // Had to modify the attribute list due to use of color space.
808 // Use modified list from here on.
809 attrib_list = strippedAttribList.data();
812 EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
813 dp->disp.dpy, config, attrib_list);
814 if (surface != EGL_NO_SURFACE) {
815 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
819 return EGL_NO_SURFACE;
822 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
826 const egl_display_ptr dp = validate_display(dpy);
827 if (!dp) return EGL_FALSE;
829 SurfaceRef _s(dp.get(), surface);
831 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
833 egl_surface_t * const s = get_surface(surface);
834 EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
835 if (result == EGL_TRUE) {
841 EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
842 EGLint attribute, EGLint *value)
846 const egl_display_ptr dp = validate_display(dpy);
847 if (!dp) return EGL_FALSE;
849 SurfaceRef _s(dp.get(), surface);
851 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
853 egl_surface_t const * const s = get_surface(surface);
854 if (attribute == EGL_GL_COLORSPACE_KHR) {
855 *value = s->getColorSpace();
858 return s->cnx->egl.eglQuerySurface(
859 dp->disp.dpy, s->surface, attribute, value);
862 void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
866 const egl_display_ptr dp = validate_display(dpy);
871 SurfaceRef _s(dp.get(), surface);
873 setError(EGL_BAD_SURFACE, EGL_FALSE);
877 // ----------------------------------------------------------------------------
879 // ----------------------------------------------------------------------------
881 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
882 EGLContext share_list, const EGLint *attrib_list)
886 egl_connection_t* cnx = NULL;
887 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
889 if (share_list != EGL_NO_CONTEXT) {
890 if (!ContextRef(dp.get(), share_list).get()) {
891 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
893 egl_context_t* const c = get_context(share_list);
894 share_list = c->context;
896 EGLContext context = cnx->egl.eglCreateContext(
897 dp->disp.dpy, config, share_list, attrib_list);
898 if (context != EGL_NO_CONTEXT) {
899 // figure out if it's a GLESv1 or GLESv2
902 while (*attrib_list != EGL_NONE) {
903 GLint attr = *attrib_list++;
904 GLint value = *attrib_list++;
905 if (attr == EGL_CONTEXT_CLIENT_VERSION) {
907 version = egl_connection_t::GLESv1_INDEX;
908 } else if (value == 2 || value == 3) {
909 version = egl_connection_t::GLESv2_INDEX;
914 egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
919 return EGL_NO_CONTEXT;
922 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
926 const egl_display_ptr dp = validate_display(dpy);
930 ContextRef _c(dp.get(), ctx);
932 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
934 egl_context_t * const c = get_context(ctx);
935 EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
936 if (result == EGL_TRUE) {
942 EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw,
943 EGLSurface read, EGLContext ctx)
947 egl_display_ptr dp = validate_display(dpy);
948 if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
950 // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
951 // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
952 // a valid but uninitialized display.
953 if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
954 (draw != EGL_NO_SURFACE) ) {
955 if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
958 // get a reference to the object passed in
959 ContextRef _c(dp.get(), ctx);
960 SurfaceRef _d(dp.get(), draw);
961 SurfaceRef _r(dp.get(), read);
963 // validate the context (if not EGL_NO_CONTEXT)
964 if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
965 // EGL_NO_CONTEXT is valid
966 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
969 // these are the underlying implementation's object
970 EGLContext impl_ctx = EGL_NO_CONTEXT;
971 EGLSurface impl_draw = EGL_NO_SURFACE;
972 EGLSurface impl_read = EGL_NO_SURFACE;
974 // these are our objects structs passed in
975 egl_context_t * c = NULL;
976 egl_surface_t const * d = NULL;
977 egl_surface_t const * r = NULL;
979 // these are the current objects structs
980 egl_context_t * cur_c = get_context(getContext());
982 if (ctx != EGL_NO_CONTEXT) {
983 c = get_context(ctx);
984 impl_ctx = c->context;
986 // no context given, use the implementation of the current context
987 if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
988 // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
989 return setError(EGL_BAD_MATCH, (EGLBoolean)EGL_FALSE);
992 // no current context
993 // not an error, there is just no current context.
998 // retrieve the underlying implementation's draw EGLSurface
999 if (draw != EGL_NO_SURFACE) {
1000 if (!_d.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1001 d = get_surface(draw);
1002 impl_draw = d->surface;
1005 // retrieve the underlying implementation's read EGLSurface
1006 if (read != EGL_NO_SURFACE) {
1007 if (!_r.get()) return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1008 r = get_surface(read);
1009 impl_read = r->surface;
1013 EGLBoolean result = dp->makeCurrent(c, cur_c,
1015 impl_draw, impl_read, impl_ctx);
1017 if (result == EGL_TRUE) {
1019 setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1020 egl_tls_t::setContext(ctx);
1025 setGLHooksThreadSpecific(&gHooksNoContext);
1026 egl_tls_t::setContext(EGL_NO_CONTEXT);
1030 if (cur_c != NULL) {
1031 // Force return to current context for drivers that cannot handle errors
1032 EGLBoolean restore_result = EGL_FALSE;
1033 // get a reference to the old current objects
1034 ContextRef _c2(dp.get(), cur_c);
1035 SurfaceRef _d2(dp.get(), cur_c->draw);
1036 SurfaceRef _r2(dp.get(), cur_c->read);
1039 impl_ctx = c->context;
1040 impl_draw = EGL_NO_SURFACE;
1041 if (cur_c->draw != EGL_NO_SURFACE) {
1042 d = get_surface(cur_c->draw);
1043 impl_draw = d->surface;
1045 impl_read = EGL_NO_SURFACE;
1046 if (cur_c->read != EGL_NO_SURFACE) {
1047 r = get_surface(cur_c->read);
1048 impl_read = r->surface;
1050 restore_result = dp->makeCurrent(c, cur_c,
1051 cur_c->draw, cur_c->read, cur_c->context,
1052 impl_draw, impl_read, impl_ctx);
1053 if (restore_result == EGL_TRUE) {
1058 ALOGE("Could not restore original EGL context");
1061 // this will ALOGE the error
1062 egl_connection_t* const cnx = &gEGLImpl;
1063 result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
1069 EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1070 EGLint attribute, EGLint *value)
1074 const egl_display_ptr dp = validate_display(dpy);
1075 if (!dp) return EGL_FALSE;
1077 ContextRef _c(dp.get(), ctx);
1078 if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1080 egl_context_t * const c = get_context(ctx);
1081 return c->cnx->egl.eglQueryContext(
1082 dp->disp.dpy, c->context, attribute, value);
1086 EGLContext eglGetCurrentContext(void)
1088 // could be called before eglInitialize(), but we wouldn't have a context
1089 // then, and this function would correctly return EGL_NO_CONTEXT.
1093 EGLContext ctx = getContext();
1097 EGLSurface eglGetCurrentSurface(EGLint readdraw)
1099 // could be called before eglInitialize(), but we wouldn't have a context
1100 // then, and this function would correctly return EGL_NO_SURFACE.
1104 EGLContext ctx = getContext();
1106 egl_context_t const * const c = get_context(ctx);
1107 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1109 case EGL_READ: return c->read;
1110 case EGL_DRAW: return c->draw;
1111 default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1114 return EGL_NO_SURFACE;
1117 EGLDisplay eglGetCurrentDisplay(void)
1119 // could be called before eglInitialize(), but we wouldn't have a context
1120 // then, and this function would correctly return EGL_NO_DISPLAY.
1124 EGLContext ctx = getContext();
1126 egl_context_t const * const c = get_context(ctx);
1127 if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1130 return EGL_NO_DISPLAY;
1133 EGLBoolean eglWaitGL(void)
1137 egl_connection_t* const cnx = &gEGLImpl;
1139 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1141 return cnx->egl.eglWaitGL();
1144 EGLBoolean eglWaitNative(EGLint engine)
1148 egl_connection_t* const cnx = &gEGLImpl;
1150 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1152 return cnx->egl.eglWaitNative(engine);
1155 EGLint eglGetError(void)
1157 EGLint err = EGL_SUCCESS;
1158 egl_connection_t* const cnx = &gEGLImpl;
1160 err = cnx->egl.eglGetError();
1162 if (err == EGL_SUCCESS) {
1163 err = egl_tls_t::getError();
1168 static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
1169 const char* procname) {
1170 const egl_connection_t* cnx = &gEGLImpl;
1173 proc = dlsym(cnx->libEgl, procname);
1174 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1176 proc = dlsym(cnx->libGles2, procname);
1177 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1179 proc = dlsym(cnx->libGles1, procname);
1180 if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1185 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1187 // eglGetProcAddress() could be the very first function called
1188 // in which case we must make sure we've initialized ourselves, this
1189 // happens the first time egl_get_display() is called.
1193 if (egl_init_drivers() == EGL_FALSE) {
1194 setError(EGL_BAD_PARAMETER, NULL);
1198 if (FILTER_EXTENSIONS(procname)) {
1202 __eglMustCastToProperFunctionPointerType addr;
1203 addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
1204 if (addr) return addr;
1206 addr = findBuiltinWrapper(procname);
1207 if (addr) return addr;
1209 // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1210 pthread_mutex_lock(&sExtensionMapMutex);
1213 * Since eglGetProcAddress() is not associated to anything, it needs
1214 * to return a function pointer that "works" regardless of what
1215 * the current context is.
1217 * For this reason, we return a "forwarder", a small stub that takes
1218 * care of calling the function associated with the context
1221 * We first look for extensions we've already resolved, if we're seeing
1222 * this extension for the first time, we go through all our
1223 * implementations and call eglGetProcAddress() and record the
1224 * result in the appropriate implementation hooks and return the
1225 * address of the forwarder corresponding to that hook set.
1229 const std::string name(procname);
1231 auto& extentionMap = sGLExtentionMap;
1232 auto pos = extentionMap.find(name);
1233 addr = (pos != extentionMap.end()) ? pos->second : nullptr;
1234 const int slot = sGLExtentionSlot;
1236 ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1237 "no more slots for eglGetProcAddress(\"%s\")",
1240 if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1243 egl_connection_t* const cnx = &gEGLImpl;
1244 if (cnx->dso && cnx->egl.eglGetProcAddress) {
1245 // Extensions are independent of the bound context
1247 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
1248 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
1249 cnx->egl.eglGetProcAddress(procname);
1250 if (addr) found = true;
1254 addr = gExtensionForwarders[slot];
1255 extentionMap[name] = addr;
1260 pthread_mutex_unlock(&sExtensionMapMutex);
1264 class FrameCompletionThread {
1267 static void queueSync(EGLSyncKHR sync) {
1268 static FrameCompletionThread thread;
1272 std::lock_guard<std::mutex> lock(thread.mMutex);
1273 snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1276 thread.mQueue.push_back(sync);
1277 thread.mCondition.notify_one();
1278 thread.mFramesQueued++;
1279 ATRACE_INT("GPU Frames Outstanding", int32_t(thread.mQueue.size()));
1284 FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1285 std::thread thread(&FrameCompletionThread::loop, this);
1289 #pragma clang diagnostic push
1290 #pragma clang diagnostic ignored "-Wmissing-noreturn"
1296 #pragma clang diagnostic pop
1302 std::unique_lock<std::mutex> lock(mMutex);
1303 while (mQueue.empty()) {
1304 mCondition.wait(lock);
1307 frameNum = mFramesCompleted;
1309 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1312 snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1315 EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1316 if (result == EGL_FALSE) {
1317 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1318 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1319 ALOGE("FrameCompletion: timeout waiting for fence");
1321 eglDestroySyncKHR(dpy, sync);
1324 std::lock_guard<std::mutex> lock(mMutex);
1327 ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
1331 uint32_t mFramesQueued;
1332 uint32_t mFramesCompleted;
1333 std::deque<EGLSyncKHR> mQueue;
1334 std::condition_variable mCondition;
1338 EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1339 EGLint *rects, EGLint n_rects)
1344 const egl_display_ptr dp = validate_display(dpy);
1345 if (!dp) return EGL_FALSE;
1347 SurfaceRef _s(dp.get(), draw);
1349 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1351 egl_surface_t const * const s = get_surface(draw);
1353 if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1354 EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1355 if (sync != EGL_NO_SYNC_KHR) {
1356 FrameCompletionThread::queueSync(sync);
1360 if (CC_UNLIKELY(dp->finishOnSwap)) {
1362 egl_context_t * const c = get_context( egl_tls_t::getContext() );
1364 // glReadPixels() ensures that the frame is complete
1365 s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1366 GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1371 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1374 std::vector<android_native_rect_t> androidRects((size_t)n_rects);
1375 for (int r = 0; r < n_rects; ++r) {
1377 int x = rects[offset];
1378 int y = rects[offset + 1];
1379 int width = rects[offset + 2];
1380 int height = rects[offset + 3];
1381 android_native_rect_t androidRect;
1382 androidRect.left = x;
1383 androidRect.top = y + height;
1384 androidRect.right = x + width;
1385 androidRect.bottom = y;
1386 androidRects.push_back(androidRect);
1388 native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
1390 if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1391 return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1394 return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1398 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1400 return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
1403 EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface,
1404 NativePixmapType target)
1408 const egl_display_ptr dp = validate_display(dpy);
1409 if (!dp) return EGL_FALSE;
1411 SurfaceRef _s(dp.get(), surface);
1413 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1415 egl_surface_t const * const s = get_surface(surface);
1416 return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
1419 const char* eglQueryString(EGLDisplay dpy, EGLint name)
1423 // Generate an error quietly when client extensions (as defined by
1424 // EGL_EXT_client_extensions) are queried. We do not want to rely on
1425 // validate_display to generate the error as validate_display would log
1426 // the error, which can be misleading.
1428 // If we want to support EGL_EXT_client_extensions later, we can return
1429 // the client extension string here instead.
1430 if (dpy == EGL_NO_DISPLAY && name == EGL_EXTENSIONS)
1431 return setErrorQuiet(EGL_BAD_DISPLAY, (const char*)0);
1433 const egl_display_ptr dp = validate_display(dpy);
1434 if (!dp) return (const char *) NULL;
1438 return dp->getVendorString();
1440 return dp->getVersionString();
1441 case EGL_EXTENSIONS:
1442 return dp->getExtensionString();
1443 case EGL_CLIENT_APIS:
1444 return dp->getClientApiString();
1448 return setError(EGL_BAD_PARAMETER, (const char *)0);
1451 extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1455 const egl_display_ptr dp = validate_display(dpy);
1456 if (!dp) return (const char *) NULL;
1460 return dp->disp.queryString.vendor;
1462 return dp->disp.queryString.version;
1463 case EGL_EXTENSIONS:
1464 return dp->disp.queryString.extensions;
1465 case EGL_CLIENT_APIS:
1466 return dp->disp.queryString.clientApi;
1470 return setError(EGL_BAD_PARAMETER, (const char *)0);
1473 // ----------------------------------------------------------------------------
1475 // ----------------------------------------------------------------------------
1477 EGLBoolean eglSurfaceAttrib(
1478 EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1482 const egl_display_ptr dp = validate_display(dpy);
1483 if (!dp) return EGL_FALSE;
1485 SurfaceRef _s(dp.get(), surface);
1487 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1489 egl_surface_t * const s = get_surface(surface);
1491 if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
1492 if (!s->getNativeWindow()) {
1493 setError(EGL_BAD_SURFACE, EGL_FALSE);
1495 int err = native_window_set_auto_refresh(s->getNativeWindow(), value != 0);
1496 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1499 if (attribute == EGL_TIMESTAMPS_ANDROID) {
1500 if (!s->getNativeWindow()) {
1501 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1503 int err = native_window_enable_frame_timestamps(s->getNativeWindow(), value != 0);
1504 return (err == 0) ? EGL_TRUE : setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1507 if (s->cnx->egl.eglSurfaceAttrib) {
1508 return s->cnx->egl.eglSurfaceAttrib(
1509 dp->disp.dpy, s->surface, attribute, value);
1511 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1514 EGLBoolean eglBindTexImage(
1515 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1519 const egl_display_ptr dp = validate_display(dpy);
1520 if (!dp) return EGL_FALSE;
1522 SurfaceRef _s(dp.get(), surface);
1524 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1526 egl_surface_t const * const s = get_surface(surface);
1527 if (s->cnx->egl.eglBindTexImage) {
1528 return s->cnx->egl.eglBindTexImage(
1529 dp->disp.dpy, s->surface, buffer);
1531 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1534 EGLBoolean eglReleaseTexImage(
1535 EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1539 const egl_display_ptr dp = validate_display(dpy);
1540 if (!dp) return EGL_FALSE;
1542 SurfaceRef _s(dp.get(), surface);
1544 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1546 egl_surface_t const * const s = get_surface(surface);
1547 if (s->cnx->egl.eglReleaseTexImage) {
1548 return s->cnx->egl.eglReleaseTexImage(
1549 dp->disp.dpy, s->surface, buffer);
1551 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1554 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1558 const egl_display_ptr dp = validate_display(dpy);
1559 if (!dp) return EGL_FALSE;
1561 EGLBoolean res = EGL_TRUE;
1562 egl_connection_t* const cnx = &gEGLImpl;
1563 if (cnx->dso && cnx->egl.eglSwapInterval) {
1564 res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
1571 // ----------------------------------------------------------------------------
1573 // ----------------------------------------------------------------------------
1575 EGLBoolean eglWaitClient(void)
1579 egl_connection_t* const cnx = &gEGLImpl;
1581 return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1584 if (cnx->egl.eglWaitClient) {
1585 res = cnx->egl.eglWaitClient();
1587 res = cnx->egl.eglWaitGL();
1592 EGLBoolean eglBindAPI(EGLenum api)
1596 if (egl_init_drivers() == EGL_FALSE) {
1597 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
1600 // bind this API on all EGLs
1601 EGLBoolean res = EGL_TRUE;
1602 egl_connection_t* const cnx = &gEGLImpl;
1603 if (cnx->dso && cnx->egl.eglBindAPI) {
1604 res = cnx->egl.eglBindAPI(api);
1609 EGLenum eglQueryAPI(void)
1613 if (egl_init_drivers() == EGL_FALSE) {
1614 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
1617 egl_connection_t* const cnx = &gEGLImpl;
1618 if (cnx->dso && cnx->egl.eglQueryAPI) {
1619 return cnx->egl.eglQueryAPI();
1622 // or, it can only be OpenGL ES
1623 return EGL_OPENGL_ES_API;
1626 EGLBoolean eglReleaseThread(void)
1630 egl_connection_t* const cnx = &gEGLImpl;
1631 if (cnx->dso && cnx->egl.eglReleaseThread) {
1632 cnx->egl.eglReleaseThread();
1635 // If there is context bound to the thread, release it
1636 egl_display_t::loseCurrent(get_context(getContext()));
1638 egl_tls_t::clearTLS();
1642 EGLSurface eglCreatePbufferFromClientBuffer(
1643 EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1644 EGLConfig config, const EGLint *attrib_list)
1648 egl_connection_t* cnx = NULL;
1649 const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1650 if (!dp) return EGL_FALSE;
1651 if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1652 return cnx->egl.eglCreatePbufferFromClientBuffer(
1653 dp->disp.dpy, buftype, buffer, config, attrib_list);
1655 return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1658 // ----------------------------------------------------------------------------
1659 // EGL_EGLEXT_VERSION 3
1660 // ----------------------------------------------------------------------------
1662 EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1663 const EGLint *attrib_list)
1667 const egl_display_ptr dp = validate_display(dpy);
1668 if (!dp) return EGL_FALSE;
1670 SurfaceRef _s(dp.get(), surface);
1672 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1674 egl_surface_t const * const s = get_surface(surface);
1675 if (s->cnx->egl.eglLockSurfaceKHR) {
1676 return s->cnx->egl.eglLockSurfaceKHR(
1677 dp->disp.dpy, s->surface, attrib_list);
1679 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
1682 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1686 const egl_display_ptr dp = validate_display(dpy);
1687 if (!dp) return EGL_FALSE;
1689 SurfaceRef _s(dp.get(), surface);
1691 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1693 egl_surface_t const * const s = get_surface(surface);
1694 if (s->cnx->egl.eglUnlockSurfaceKHR) {
1695 return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
1697 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
1700 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1701 EGLClientBuffer buffer, const EGLint *attrib_list)
1705 const egl_display_ptr dp = validate_display(dpy);
1706 if (!dp) return EGL_NO_IMAGE_KHR;
1708 ContextRef _c(dp.get(), ctx);
1709 egl_context_t * const c = _c.get();
1711 EGLImageKHR result = EGL_NO_IMAGE_KHR;
1712 egl_connection_t* const cnx = &gEGLImpl;
1713 if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1714 result = cnx->egl.eglCreateImageKHR(
1716 c ? c->context : EGL_NO_CONTEXT,
1717 target, buffer, attrib_list);
1722 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1726 const egl_display_ptr dp = validate_display(dpy);
1727 if (!dp) return EGL_FALSE;
1729 EGLBoolean result = EGL_FALSE;
1730 egl_connection_t* const cnx = &gEGLImpl;
1731 if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
1732 result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
1737 // ----------------------------------------------------------------------------
1738 // EGL_EGLEXT_VERSION 5
1739 // ----------------------------------------------------------------------------
1742 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1746 const egl_display_ptr dp = validate_display(dpy);
1747 if (!dp) return EGL_NO_SYNC_KHR;
1749 EGLSyncKHR result = EGL_NO_SYNC_KHR;
1750 egl_connection_t* const cnx = &gEGLImpl;
1751 if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1752 result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
1757 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1761 const egl_display_ptr dp = validate_display(dpy);
1762 if (!dp) return EGL_FALSE;
1764 EGLBoolean result = EGL_FALSE;
1765 egl_connection_t* const cnx = &gEGLImpl;
1766 if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1767 result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
1772 EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1775 const egl_display_ptr dp = validate_display(dpy);
1776 if (!dp) return EGL_FALSE;
1778 EGLBoolean result = EGL_FALSE;
1779 egl_connection_t* const cnx = &gEGLImpl;
1780 if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1781 result = cnx->egl.eglSignalSyncKHR(
1782 dp->disp.dpy, sync, mode);
1787 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1788 EGLint flags, EGLTimeKHR timeout)
1792 const egl_display_ptr dp = validate_display(dpy);
1793 if (!dp) return EGL_FALSE;
1795 EGLint result = EGL_FALSE;
1796 egl_connection_t* const cnx = &gEGLImpl;
1797 if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1798 result = cnx->egl.eglClientWaitSyncKHR(
1799 dp->disp.dpy, sync, flags, timeout);
1804 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1805 EGLint attribute, EGLint *value)
1809 const egl_display_ptr dp = validate_display(dpy);
1810 if (!dp) return EGL_FALSE;
1812 EGLBoolean result = EGL_FALSE;
1813 egl_connection_t* const cnx = &gEGLImpl;
1814 if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1815 result = cnx->egl.eglGetSyncAttribKHR(
1816 dp->disp.dpy, sync, attribute, value);
1821 EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1825 const egl_display_ptr dp = validate_display(dpy);
1826 if (!dp) return EGL_NO_STREAM_KHR;
1828 EGLStreamKHR result = EGL_NO_STREAM_KHR;
1829 egl_connection_t* const cnx = &gEGLImpl;
1830 if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1831 result = cnx->egl.eglCreateStreamKHR(
1832 dp->disp.dpy, attrib_list);
1837 EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1841 const egl_display_ptr dp = validate_display(dpy);
1842 if (!dp) return EGL_FALSE;
1844 EGLBoolean result = EGL_FALSE;
1845 egl_connection_t* const cnx = &gEGLImpl;
1846 if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1847 result = cnx->egl.eglDestroyStreamKHR(
1848 dp->disp.dpy, stream);
1853 EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1854 EGLenum attribute, EGLint value)
1858 const egl_display_ptr dp = validate_display(dpy);
1859 if (!dp) return EGL_FALSE;
1861 EGLBoolean result = EGL_FALSE;
1862 egl_connection_t* const cnx = &gEGLImpl;
1863 if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1864 result = cnx->egl.eglStreamAttribKHR(
1865 dp->disp.dpy, stream, attribute, value);
1870 EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1871 EGLenum attribute, EGLint *value)
1875 const egl_display_ptr dp = validate_display(dpy);
1876 if (!dp) return EGL_FALSE;
1878 EGLBoolean result = EGL_FALSE;
1879 egl_connection_t* const cnx = &gEGLImpl;
1880 if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1881 result = cnx->egl.eglQueryStreamKHR(
1882 dp->disp.dpy, stream, attribute, value);
1887 EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1888 EGLenum attribute, EGLuint64KHR *value)
1892 const egl_display_ptr dp = validate_display(dpy);
1893 if (!dp) return EGL_FALSE;
1895 EGLBoolean result = EGL_FALSE;
1896 egl_connection_t* const cnx = &gEGLImpl;
1897 if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1898 result = cnx->egl.eglQueryStreamu64KHR(
1899 dp->disp.dpy, stream, attribute, value);
1904 EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1905 EGLenum attribute, EGLTimeKHR *value)
1909 const egl_display_ptr dp = validate_display(dpy);
1910 if (!dp) return EGL_FALSE;
1912 EGLBoolean result = EGL_FALSE;
1913 egl_connection_t* const cnx = &gEGLImpl;
1914 if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1915 result = cnx->egl.eglQueryStreamTimeKHR(
1916 dp->disp.dpy, stream, attribute, value);
1921 EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1922 EGLStreamKHR stream, const EGLint *attrib_list)
1926 egl_display_ptr dp = validate_display(dpy);
1927 if (!dp) return EGL_NO_SURFACE;
1929 EGLint colorSpace = EGL_GL_COLORSPACE_LINEAR_KHR;
1930 android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
1931 // TODO: Probably need to update EGL_KHR_stream_producer_eglsurface to
1932 // indicate support for EGL_GL_COLORSPACE_KHR.
1933 // now select a corresponding sRGB format if needed
1934 if (!getColorSpaceAttribute(dp, attrib_list, colorSpace, dataSpace)) {
1935 ALOGE("error invalid colorspace: %d", colorSpace);
1936 return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
1939 egl_connection_t* const cnx = &gEGLImpl;
1940 if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1941 EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1942 dp->disp.dpy, config, stream, attrib_list);
1943 if (surface != EGL_NO_SURFACE) {
1944 egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, surface, colorSpace, cnx);
1948 return EGL_NO_SURFACE;
1951 EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1952 EGLStreamKHR stream)
1956 const egl_display_ptr dp = validate_display(dpy);
1957 if (!dp) return EGL_FALSE;
1959 EGLBoolean result = EGL_FALSE;
1960 egl_connection_t* const cnx = &gEGLImpl;
1961 if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1962 result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1963 dp->disp.dpy, stream);
1968 EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1969 EGLStreamKHR stream)
1973 const egl_display_ptr dp = validate_display(dpy);
1974 if (!dp) return EGL_FALSE;
1976 EGLBoolean result = EGL_FALSE;
1977 egl_connection_t* const cnx = &gEGLImpl;
1978 if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1979 result = cnx->egl.eglStreamConsumerAcquireKHR(
1980 dp->disp.dpy, stream);
1985 EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1986 EGLStreamKHR stream)
1990 const egl_display_ptr dp = validate_display(dpy);
1991 if (!dp) return EGL_FALSE;
1993 EGLBoolean result = EGL_FALSE;
1994 egl_connection_t* const cnx = &gEGLImpl;
1995 if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1996 result = cnx->egl.eglStreamConsumerReleaseKHR(
1997 dp->disp.dpy, stream);
2002 EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2003 EGLDisplay dpy, EGLStreamKHR stream)
2007 const egl_display_ptr dp = validate_display(dpy);
2008 if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2010 EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
2011 egl_connection_t* const cnx = &gEGLImpl;
2012 if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
2013 result = cnx->egl.eglGetStreamFileDescriptorKHR(
2014 dp->disp.dpy, stream);
2019 EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2020 EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2024 const egl_display_ptr dp = validate_display(dpy);
2025 if (!dp) return EGL_NO_STREAM_KHR;
2027 EGLStreamKHR result = EGL_NO_STREAM_KHR;
2028 egl_connection_t* const cnx = &gEGLImpl;
2029 if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
2030 result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
2031 dp->disp.dpy, file_descriptor);
2036 // ----------------------------------------------------------------------------
2037 // EGL_EGLEXT_VERSION 15
2038 // ----------------------------------------------------------------------------
2040 EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2042 const egl_display_ptr dp = validate_display(dpy);
2043 if (!dp) return EGL_FALSE;
2044 EGLint result = EGL_FALSE;
2045 egl_connection_t* const cnx = &gEGLImpl;
2046 if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
2047 result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
2052 // ----------------------------------------------------------------------------
2053 // ANDROID extensions
2054 // ----------------------------------------------------------------------------
2056 EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2060 const egl_display_ptr dp = validate_display(dpy);
2061 if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2063 EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
2064 egl_connection_t* const cnx = &gEGLImpl;
2065 if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
2066 result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
2071 EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2072 EGLnsecsANDROID time)
2076 const egl_display_ptr dp = validate_display(dpy);
2081 SurfaceRef _s(dp.get(), surface);
2083 setError(EGL_BAD_SURFACE, EGL_FALSE);
2087 egl_surface_t const * const s = get_surface(surface);
2088 native_window_set_buffers_timestamp(s->getNativeWindow(), time);
2093 EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2095 // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2096 // this function cannot be implemented when this libEGL is built for
2098 #ifndef __ANDROID_VNDK__
2099 if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2100 return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
2102 return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2106 // ----------------------------------------------------------------------------
2107 // NVIDIA extensions
2108 // ----------------------------------------------------------------------------
2109 EGLuint64NV eglGetSystemTimeFrequencyNV()
2113 if (egl_init_drivers() == EGL_FALSE) {
2114 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
2117 EGLuint64NV ret = 0;
2118 egl_connection_t* const cnx = &gEGLImpl;
2120 if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2121 return cnx->egl.eglGetSystemTimeFrequencyNV();
2124 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
2127 EGLuint64NV eglGetSystemTimeNV()
2131 if (egl_init_drivers() == EGL_FALSE) {
2132 return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
2135 EGLuint64NV ret = 0;
2136 egl_connection_t* const cnx = &gEGLImpl;
2138 if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2139 return cnx->egl.eglGetSystemTimeNV();
2142 return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
2145 // ----------------------------------------------------------------------------
2146 // Partial update extension
2147 // ----------------------------------------------------------------------------
2148 EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2149 EGLint *rects, EGLint n_rects)
2153 const egl_display_ptr dp = validate_display(dpy);
2155 setError(EGL_BAD_DISPLAY, EGL_FALSE);
2159 SurfaceRef _s(dp.get(), surface);
2161 setError(EGL_BAD_SURFACE, EGL_FALSE);
2165 egl_surface_t const * const s = get_surface(surface);
2166 if (s->cnx->egl.eglSetDamageRegionKHR) {
2167 return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
2174 EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2175 EGLuint64KHR *frameId) {
2178 const egl_display_ptr dp = validate_display(dpy);
2180 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2183 SurfaceRef _s(dp.get(), surface);
2185 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2188 egl_surface_t const * const s = get_surface(surface);
2190 if (!s->getNativeWindow()) {
2191 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2194 uint64_t nextFrameId = 0;
2195 int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
2198 // This should not happen. Return an error that is not in the spec
2199 // so it's obvious something is very wrong.
2200 ALOGE("eglGetNextFrameId: Unexpected error.");
2201 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
2204 *frameId = nextFrameId;
2208 EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2209 EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2213 const egl_display_ptr dp = validate_display(dpy);
2215 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2218 SurfaceRef _s(dp.get(), surface);
2220 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2223 egl_surface_t const * const s = get_surface(surface);
2225 if (!s->getNativeWindow()) {
2226 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2229 nsecs_t* compositeDeadline = nullptr;
2230 nsecs_t* compositeInterval = nullptr;
2231 nsecs_t* compositeToPresentLatency = nullptr;
2233 for (int i = 0; i < numTimestamps; i++) {
2235 case EGL_COMPOSITE_DEADLINE_ANDROID:
2236 compositeDeadline = &values[i];
2238 case EGL_COMPOSITE_INTERVAL_ANDROID:
2239 compositeInterval = &values[i];
2241 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2242 compositeToPresentLatency = &values[i];
2245 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2249 int ret = native_window_get_compositor_timing(s->getNativeWindow(),
2250 compositeDeadline, compositeInterval, compositeToPresentLatency);
2256 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2258 // This should not happen. Return an error that is not in the spec
2259 // so it's obvious something is very wrong.
2260 ALOGE("eglGetCompositorTiming: Unexpected error.");
2261 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
2265 EGLBoolean eglGetCompositorTimingSupportedANDROID(
2266 EGLDisplay dpy, EGLSurface surface, EGLint name)
2270 const egl_display_ptr dp = validate_display(dpy);
2272 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2275 SurfaceRef _s(dp.get(), surface);
2277 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2280 egl_surface_t const * const s = get_surface(surface);
2282 ANativeWindow* window = s->getNativeWindow();
2284 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2288 case EGL_COMPOSITE_DEADLINE_ANDROID:
2289 case EGL_COMPOSITE_INTERVAL_ANDROID:
2290 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2297 EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
2298 EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
2299 EGLnsecsANDROID *values)
2303 const egl_display_ptr dp = validate_display(dpy);
2305 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2308 SurfaceRef _s(dp.get(), surface);
2310 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2313 egl_surface_t const * const s = get_surface(surface);
2315 if (!s->getNativeWindow()) {
2316 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2319 nsecs_t* requestedPresentTime = nullptr;
2320 nsecs_t* acquireTime = nullptr;
2321 nsecs_t* latchTime = nullptr;
2322 nsecs_t* firstRefreshStartTime = nullptr;
2323 nsecs_t* gpuCompositionDoneTime = nullptr;
2324 nsecs_t* lastRefreshStartTime = nullptr;
2325 nsecs_t* displayPresentTime = nullptr;
2326 nsecs_t* dequeueReadyTime = nullptr;
2327 nsecs_t* releaseTime = nullptr;
2329 for (int i = 0; i < numTimestamps; i++) {
2330 switch (timestamps[i]) {
2331 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2332 requestedPresentTime = &values[i];
2334 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2335 acquireTime = &values[i];
2337 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2338 latchTime = &values[i];
2340 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2341 firstRefreshStartTime = &values[i];
2343 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2344 lastRefreshStartTime = &values[i];
2346 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2347 gpuCompositionDoneTime = &values[i];
2349 case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2350 displayPresentTime = &values[i];
2352 case EGL_DEQUEUE_READY_TIME_ANDROID:
2353 dequeueReadyTime = &values[i];
2355 case EGL_READS_DONE_TIME_ANDROID:
2356 releaseTime = &values[i];
2359 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2363 int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
2364 requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
2365 lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
2366 dequeueReadyTime, releaseTime);
2372 return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
2374 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2376 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2378 // This should not happen. Return an error that is not in the spec
2379 // so it's obvious something is very wrong.
2380 ALOGE("eglGetFrameTimestamps: Unexpected error.");
2381 return setError(EGL_NOT_INITIALIZED, (EGLBoolean)EGL_FALSE);
2385 EGLBoolean eglGetFrameTimestampSupportedANDROID(
2386 EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
2390 const egl_display_ptr dp = validate_display(dpy);
2392 return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2395 SurfaceRef _s(dp.get(), surface);
2397 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2400 egl_surface_t const * const s = get_surface(surface);
2402 ANativeWindow* window = s->getNativeWindow();
2404 return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2407 switch (timestamp) {
2408 case EGL_COMPOSITE_DEADLINE_ANDROID:
2409 case EGL_COMPOSITE_INTERVAL_ANDROID:
2410 case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2411 case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2412 case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2413 case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2414 case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2415 case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2416 case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2417 case EGL_DEQUEUE_READY_TIME_ANDROID:
2418 case EGL_READS_DONE_TIME_ANDROID:
2420 case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2422 window->query(window,
2423 NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2424 return value == 0 ? EGL_FALSE : EGL_TRUE;