OSDN Git Service

libEGL: select pixel format by EGL_NATIVE_VISUAL_ID
[android-x86/frameworks-native.git] / opengl / libs / EGL / eglApi.cpp
1 /*
2  ** Copyright 2007, The Android Open Source Project
3  **
4  ** Licensed under the Apache License, Version 2.0 (the "License");
5  ** you may not use this file except in compliance with the License.
6  ** You may obtain a copy of the License at
7  **
8  **     http://www.apache.org/licenses/LICENSE-2.0
9  **
10  ** Unless required by applicable law or agreed to in writing, software
11  ** distributed under the License is distributed on an "AS IS" BASIS,
12  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  ** See the License for the specific language governing permissions and
14  ** limitations under the License.
15  */
16
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
19 #include <ctype.h>
20 #include <dlfcn.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <hardware/gralloc1.h>
25
26 #include <EGL/egl.h>
27 #include <EGL/eglext.h>
28
29 #include <android/hardware_buffer.h>
30 #include <private/android/AHardwareBufferHelpers.h>
31
32 #include <cutils/compiler.h>
33 #include <cutils/properties.h>
34 #include <log/log.h>
35
36 #include <condition_variable>
37 #include <deque>
38 #include <mutex>
39 #include <unordered_map>
40 #include <string>
41 #include <thread>
42
43 #include "../egl_impl.h"
44
45 #include "egl_display.h"
46 #include "egl_object.h"
47 #include "egl_tls.h"
48 #include "egl_trace.h"
49
50 using namespace android;
51
52 // ----------------------------------------------------------------------------
53
54 namespace android {
55
56 using nsecs_t = int64_t;
57
58 struct extention_map_t {
59     const char* name;
60     __eglMustCastToProperFunctionPointerType address;
61 };
62
63 /*
64  * This is the list of EGL extensions exposed to applications.
65  *
66  * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
67  * wrapper and are always available.
68  *
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
74  * are missing.
75  *
76  * NOTE: Both strings MUST have a single space as the last character.
77  */
78
79 extern char const * const gBuiltinExtensionString;
80 extern char const * const gExtensionString;
81
82 // clang-format off
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         ;
91
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 "
107         "EGL_KHR_stream "
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 "
126         ;
127 // clang-format on
128
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
135
136 /*
137  * EGL Extensions entry-points exposed to 3rd party applications
138  * (keep in sync with gExtensionString above)
139  *
140  */
141 static const extention_map_t sExtensionMap[] = {
142     // EGL_KHR_lock_surface
143     { "eglLockSurfaceKHR",
144             (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
145     { "eglUnlockSurfaceKHR",
146             (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
147
148     // EGL_KHR_image, EGL_KHR_image_base
149     { "eglCreateImageKHR",
150             (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
151     { "eglDestroyImageKHR",
152             (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
153
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 },
165
166     // EGL_NV_system_time
167     { "eglGetSystemTimeFrequencyNV",
168             (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
169     { "eglGetSystemTimeNV",
170             (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
171
172     // EGL_KHR_wait_sync
173     { "eglWaitSyncKHR",
174             (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
175
176     // EGL_ANDROID_presentation_time
177     { "eglPresentationTimeANDROID",
178             (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
179
180     // EGL_KHR_swap_buffers_with_damage
181     { "eglSwapBuffersWithDamageKHR",
182             (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
183
184     // EGL_ANDROID_get_native_client_buffer
185     { "eglGetNativeClientBufferANDROID",
186             (__eglMustCastToProperFunctionPointerType)&eglGetNativeClientBufferANDROID },
187
188     // EGL_KHR_partial_update
189     { "eglSetDamageRegionKHR",
190             (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
191
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 },
216
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 },
228
229     // EGL_ANDROID_native_fence_sync
230     { "eglDupNativeFenceFDANDROID",
231             (__eglMustCastToProperFunctionPointerType)&eglDupNativeFenceFDANDROID },
232 };
233
234 /*
235  * These extensions entry-points should not be exposed to applications.
236  * They're used internally by the Android EGL layer.
237  */
238 #define FILTER_EXTENSIONS(procname) \
239         (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") ||    \
240          !strcmp((procname), "eglHibernateProcessIMG")      ||    \
241          !strcmp((procname), "eglAwakenProcessIMG"))
242
243
244
245 // accesses protected by sExtensionMapMutex
246 static std::unordered_map<std::string, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
247
248 static int sGLExtentionSlot = 0;
249 static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
250
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;
256         }
257     }
258     return NULL;
259 }
260
261 // ----------------------------------------------------------------------------
262
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;
267
268 } // namespace android;
269
270
271 // ----------------------------------------------------------------------------
272
273 static inline void clearError() { egl_tls_t::clearError(); }
274 static inline EGLContext getContext() { return egl_tls_t::getContext(); }
275
276 // ----------------------------------------------------------------------------
277
278 EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
279 {
280     ATRACE_CALL();
281     clearError();
282
283     uintptr_t index = reinterpret_cast<uintptr_t>(display);
284     if (index >= NUM_DISPLAYS) {
285         return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
286     }
287
288     if (egl_init_drivers() == EGL_FALSE) {
289         return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
290     }
291
292     EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
293     return dpy;
294 }
295
296 // ----------------------------------------------------------------------------
297 // Initialization
298 // ----------------------------------------------------------------------------
299
300 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
301 {
302     clearError();
303
304     egl_display_ptr dp = get_display(dpy);
305     if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
306
307     EGLBoolean res = dp->initialize(major, minor);
308
309     return res;
310 }
311
312 EGLBoolean eglTerminate(EGLDisplay dpy)
313 {
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.
317
318     clearError();
319
320     egl_display_ptr dp = get_display(dpy);
321     if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
322
323     EGLBoolean res = dp->terminate();
324
325     return res;
326 }
327
328 // ----------------------------------------------------------------------------
329 // configuration
330 // ----------------------------------------------------------------------------
331
332 EGLBoolean eglGetConfigs(   EGLDisplay dpy,
333                             EGLConfig *configs,
334                             EGLint config_size, EGLint *num_config)
335 {
336     clearError();
337
338     const egl_display_ptr dp = validate_display(dpy);
339     if (!dp) return EGL_FALSE;
340
341     if (num_config==0) {
342         return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
343     }
344
345     EGLBoolean res = EGL_FALSE;
346     *num_config = 0;
347
348     egl_connection_t* const cnx = &gEGLImpl;
349     if (cnx->dso) {
350         res = cnx->egl.eglGetConfigs(
351                 dp->disp.dpy, configs, config_size, num_config);
352     }
353
354     return res;
355 }
356
357 EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
358                             EGLConfig *configs, EGLint config_size,
359                             EGLint *num_config)
360 {
361     clearError();
362
363     const egl_display_ptr dp = validate_display(dpy);
364     if (!dp) return EGL_FALSE;
365
366     if (num_config==0) {
367         return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
368     }
369
370     EGLBoolean res = EGL_FALSE;
371     *num_config = 0;
372
373     egl_connection_t* const cnx = &gEGLImpl;
374     if (cnx->dso) {
375         if (attrib_list) {
376             char value[PROPERTY_VALUE_MAX];
377             property_get("debug.egl.force_msaa", value, "false");
378
379             if (!strcmp(value, "true")) {
380                 size_t attribCount = 0;
381                 EGLint attrib = attrib_list[0];
382
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;
387
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];
392                     switch (attrib) {
393                         case EGL_RENDERABLE_TYPE:
394                             attribRendererable = &attrib_list[attribCount];
395                             break;
396                         case EGL_CONFIG_CAVEAT:
397                             attribCaveat = &attrib_list[attribCount];
398                             break;
399                         default:
400                             break;
401                     }
402                     attribCount++;
403                 }
404
405                 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
406                         (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
407
408                     // Insert 2 extra attributes to force-enable MSAA 4x
409                     EGLint aaAttribs[attribCount + 4];
410                     aaAttribs[0] = EGL_SAMPLE_BUFFERS;
411                     aaAttribs[1] = 1;
412                     aaAttribs[2] = EGL_SAMPLES;
413                     aaAttribs[3] = 4;
414
415                     memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
416
417                     EGLint numConfigAA;
418                     EGLBoolean resAA = cnx->egl.eglChooseConfig(
419                             dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
420
421                     if (resAA == EGL_TRUE && numConfigAA > 0) {
422                         ALOGD("Enabling MSAA 4x");
423                         *num_config = numConfigAA;
424                         return resAA;
425                     }
426                 }
427             }
428         }
429
430         res = cnx->egl.eglChooseConfig(
431                 dp->disp.dpy, attrib_list, configs, config_size, num_config);
432     }
433     return res;
434 }
435
436 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
437         EGLint attribute, EGLint *value)
438 {
439     clearError();
440
441     egl_connection_t* cnx = NULL;
442     const egl_display_ptr dp = validate_display_connection(dpy, cnx);
443     if (!dp) return EGL_FALSE;
444
445     return cnx->egl.eglGetConfigAttrib(
446             dp->disp.dpy, config, attribute, value);
447 }
448
449 // ----------------------------------------------------------------------------
450 // surfaces
451 // ----------------------------------------------------------------------------
452
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,
458                                                EGLint colorspace) {
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;
471     }
472     return dataSpace;
473 }
474
475 // Return true if we stripped any EGL_GL_COLORSPACE_KHR attributes.
476 static EGLBoolean stripColorSpaceAttribute(egl_display_ptr dp, const EGLint* attrib_list,
477                                            EGLint format,
478                                            std::vector<EGLint>& stripped_attrib_list) {
479     std::vector<EGLint> allowedColorSpaces;
480     switch (format) {
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);
487             break;
488
489         case HAL_PIXEL_FORMAT_RGBA_FP16:
490         case HAL_PIXEL_FORMAT_RGBA_1010102:
491         default:
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
494             break;
495     }
496
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];
502                 bool found = false;
503                 // Verify that color space is allowed
504                 for (auto it : allowedColorSpaces) {
505                     if (colorSpace == it) {
506                         found = true;
507                     }
508                 }
509                 if (!found) {
510                     stripped = true;
511                 } else {
512                     stripped_attrib_list.push_back(attr[0]);
513                     stripped_attrib_list.push_back(attr[1]);
514                 }
515             } else {
516                 stripped_attrib_list.push_back(attr[0]);
517                 stripped_attrib_list.push_back(attr[1]);
518             }
519         }
520     }
521     if (stripped) {
522         stripped_attrib_list.push_back(EGL_NONE);
523         stripped_attrib_list.push_back(EGL_NONE);
524     }
525     return stripped;
526 }
527
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;
533
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];
538                 bool found = false;
539                 bool verify = true;
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.
545                     found = true;
546                     verify = false;
547                 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_linear &&
548                            dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_linear")) {
549                     found = true;
550                 } else if (colorSpace == EGL_EXT_gl_colorspace_bt2020_pq &&
551                            dp->haveExtension("EGL_EXT_gl_colorspace_bt2020_pq")) {
552                     found = true;
553                 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_EXT &&
554                            dp->haveExtension("EGL_EXT_gl_colorspace_scrgb")) {
555                     found = true;
556                 } else if (colorSpace == EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT &&
557                            dp->haveExtension("EGL_EXT_gl_colorspace_scrgb_linear")) {
558                     found = true;
559                 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_LINEAR_EXT &&
560                            dp->haveExtension("EGL_EXT_gl_colorspace_display_p3_linear")) {
561                     found = true;
562                 } else if (colorSpace == EGL_GL_COLORSPACE_DISPLAY_P3_EXT &&
563                            dp->haveExtension("EGL_EXT_gl_colorspace_display_p3")) {
564                     found = true;
565                 }
566                 if (!found) {
567                     return false;
568                 }
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);
581
582                     if (err) {
583                         ALOGE("getColorSpaceAttribute: invalid window (win=%p) "
584                               "failed (%#x) (already connected to another API?)",
585                               window, err);
586                         return false;
587                     }
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.
591                         return false;
592                     }
593                 }
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);
597             }
598         }
599     }
600     return true;
601 }
602
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);
606 }
607
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
613     // format.
614
615     EGLint componentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
616     cnx->egl.eglGetConfigAttrib(dpy, config, EGL_COLOR_COMPONENT_TYPE_EXT, &componentType);
617
618     EGLint a = 0;
619     EGLint r, g, b;
620     r = g = b = 0;
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;
626
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.
635     // else if 565, 4444
636     //    TBD: Can we assume these are supported if 8888 is?
637     // else if FP16 or 1010102
638     //    strip colorspace from attribs.
639     // endif
640     if (a == 0) {
641         if (colorDepth <= 16) {
642             format = HAL_PIXEL_FORMAT_RGB_565;
643         } else {
644             if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
645                 if (colorDepth > 24) {
646                     format = HAL_PIXEL_FORMAT_RGBA_1010102;
647                 } else {
648                     format = HAL_PIXEL_FORMAT_RGBX_8888;
649                 }
650             } else {
651                 format = HAL_PIXEL_FORMAT_RGBA_FP16;
652             }
653         }
654     } else {
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;
661             }
662         } else {
663             format = HAL_PIXEL_FORMAT_RGBA_FP16;
664         }
665     }
666 }
667
668 EGLSurface eglCreateWindowSurface(  EGLDisplay dpy, EGLConfig config,
669                                     NativeWindowType window,
670                                     const EGLint *attrib_list)
671 {
672     clearError();
673
674     egl_connection_t* cnx = NULL;
675     egl_display_ptr dp = validate_display_connection(dpy, cnx);
676     if (dp) {
677         EGLDisplay iDpy = dp->disp.dpy;
678
679         if (!window) {
680             return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
681         }
682
683         int value = 0;
684         window->query(window, NATIVE_WINDOW_IS_VALID, &value);
685         if (!value) {
686             return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
687         }
688
689         int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
690         if (result < 0) {
691             ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
692                     "failed (%#x) (already connected to another API?)",
693                     window, result);
694             return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
695         }
696
697         EGLint format;
698         getNativePixelFormat(iDpy, cnx, config, format);
699
700         // now select correct colorspace and dataspace based on user's attribute list
701         EGLint colorSpace;
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);
706         }
707
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();
713         }
714
715         if (format != 0) {
716             int err = native_window_set_buffers_format(window, format);
717             if (err != 0) {
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);
722             }
723         }
724
725         if (dataSpace != 0) {
726             int err = native_window_set_buffers_data_space(window, dataSpace);
727             if (err != 0) {
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);
732             }
733         }
734
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);
739
740         EGLSurface surface = cnx->egl.eglCreateWindowSurface(
741                 iDpy, config, window, attrib_list);
742         if (surface != EGL_NO_SURFACE) {
743             egl_surface_t* s =
744                     new egl_surface_t(dp.get(), config, window, surface, colorSpace, cnx);
745             return s;
746         }
747
748         // EGLSurface creation failed
749         native_window_set_buffers_format(window, 0);
750         native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
751     }
752     return EGL_NO_SURFACE;
753 }
754
755 EGLSurface eglCreatePixmapSurface(  EGLDisplay dpy, EGLConfig config,
756                                     NativePixmapType pixmap,
757                                     const EGLint *attrib_list)
758 {
759     clearError();
760
761     egl_connection_t* cnx = NULL;
762     egl_display_ptr dp = validate_display_connection(dpy, cnx);
763     EGLint colorSpace;
764     android_dataspace dataSpace;
765     if (dp) {
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);
770         }
771
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);
776             return s;
777         }
778     }
779     return EGL_NO_SURFACE;
780 }
781
782 EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
783                                     const EGLint *attrib_list)
784 {
785     clearError();
786
787     egl_connection_t* cnx = NULL;
788     egl_display_ptr dp = validate_display_connection(dpy, cnx);
789     if (dp) {
790         EGLDisplay iDpy = dp->disp.dpy;
791         EGLint format;
792         getNativePixelFormat(iDpy, cnx, config, format);
793
794         // now select correct colorspace and dataspace based on user's attribute list
795         EGLint colorSpace;
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);
800         }
801
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();
810         }
811
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);
816             return s;
817         }
818     }
819     return EGL_NO_SURFACE;
820 }
821
822 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
823 {
824     clearError();
825
826     const egl_display_ptr dp = validate_display(dpy);
827     if (!dp) return EGL_FALSE;
828
829     SurfaceRef _s(dp.get(), surface);
830     if (!_s.get())
831         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
832
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) {
836         _s.terminate();
837     }
838     return result;
839 }
840
841 EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
842                             EGLint attribute, EGLint *value)
843 {
844     clearError();
845
846     const egl_display_ptr dp = validate_display(dpy);
847     if (!dp) return EGL_FALSE;
848
849     SurfaceRef _s(dp.get(), surface);
850     if (!_s.get())
851         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
852
853     egl_surface_t const * const s = get_surface(surface);
854     if (attribute == EGL_GL_COLORSPACE_KHR) {
855         *value = s->getColorSpace();
856         return EGL_TRUE;
857     }
858     return s->cnx->egl.eglQuerySurface(
859             dp->disp.dpy, s->surface, attribute, value);
860 }
861
862 void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
863     ATRACE_CALL();
864     clearError();
865
866     const egl_display_ptr dp = validate_display(dpy);
867     if (!dp) {
868         return;
869     }
870
871     SurfaceRef _s(dp.get(), surface);
872     if (!_s.get()) {
873         setError(EGL_BAD_SURFACE, EGL_FALSE);
874     }
875 }
876
877 // ----------------------------------------------------------------------------
878 // Contexts
879 // ----------------------------------------------------------------------------
880
881 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
882                             EGLContext share_list, const EGLint *attrib_list)
883 {
884     clearError();
885
886     egl_connection_t* cnx = NULL;
887     const egl_display_ptr dp = validate_display_connection(dpy, cnx);
888     if (dp) {
889         if (share_list != EGL_NO_CONTEXT) {
890             if (!ContextRef(dp.get(), share_list).get()) {
891                 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
892             }
893             egl_context_t* const c = get_context(share_list);
894             share_list = c->context;
895         }
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
900             int version = 0;
901             if (attrib_list) {
902                 while (*attrib_list != EGL_NONE) {
903                     GLint attr = *attrib_list++;
904                     GLint value = *attrib_list++;
905                     if (attr == EGL_CONTEXT_CLIENT_VERSION) {
906                         if (value == 1) {
907                             version = egl_connection_t::GLESv1_INDEX;
908                         } else if (value == 2 || value == 3) {
909                             version = egl_connection_t::GLESv2_INDEX;
910                         }
911                     }
912                 };
913             }
914             egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
915                     version);
916             return c;
917         }
918     }
919     return EGL_NO_CONTEXT;
920 }
921
922 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
923 {
924     clearError();
925
926     const egl_display_ptr dp = validate_display(dpy);
927     if (!dp)
928         return EGL_FALSE;
929
930     ContextRef _c(dp.get(), ctx);
931     if (!_c.get())
932         return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
933
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) {
937         _c.terminate();
938     }
939     return result;
940 }
941
942 EGLBoolean eglMakeCurrent(  EGLDisplay dpy, EGLSurface draw,
943                             EGLSurface read, EGLContext ctx)
944 {
945     clearError();
946
947     egl_display_ptr dp = validate_display(dpy);
948     if (!dp) return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
949
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);
956     }
957
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);
962
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);
967     }
968
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;
973
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;
978
979     // these are the current objects structs
980     egl_context_t * cur_c = get_context(getContext());
981
982     if (ctx != EGL_NO_CONTEXT) {
983         c = get_context(ctx);
984         impl_ctx = c->context;
985     } else {
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);
990         }
991         if (cur_c == NULL) {
992             // no current context
993             // not an error, there is just no current context.
994             return EGL_TRUE;
995         }
996     }
997
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;
1003     }
1004
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;
1010     }
1011
1012
1013     EGLBoolean result = dp->makeCurrent(c, cur_c,
1014             draw, read, ctx,
1015             impl_draw, impl_read, impl_ctx);
1016
1017     if (result == EGL_TRUE) {
1018         if (c) {
1019             setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
1020             egl_tls_t::setContext(ctx);
1021             _c.acquire();
1022             _r.acquire();
1023             _d.acquire();
1024         } else {
1025             setGLHooksThreadSpecific(&gHooksNoContext);
1026             egl_tls_t::setContext(EGL_NO_CONTEXT);
1027         }
1028     } else {
1029
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);
1037
1038             c = cur_c;
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;
1044             }
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;
1049             }
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) {
1054                 _c2.acquire();
1055                 _r2.acquire();
1056                 _d2.acquire();
1057             } else {
1058                 ALOGE("Could not restore original EGL context");
1059             }
1060         }
1061         // this will ALOGE the error
1062         egl_connection_t* const cnx = &gEGLImpl;
1063         result = setError(cnx->egl.eglGetError(), (EGLBoolean)EGL_FALSE);
1064     }
1065     return result;
1066 }
1067
1068
1069 EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
1070                             EGLint attribute, EGLint *value)
1071 {
1072     clearError();
1073
1074     const egl_display_ptr dp = validate_display(dpy);
1075     if (!dp) return EGL_FALSE;
1076
1077     ContextRef _c(dp.get(), ctx);
1078     if (!_c.get()) return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1079
1080     egl_context_t * const c = get_context(ctx);
1081     return c->cnx->egl.eglQueryContext(
1082             dp->disp.dpy, c->context, attribute, value);
1083
1084 }
1085
1086 EGLContext eglGetCurrentContext(void)
1087 {
1088     // could be called before eglInitialize(), but we wouldn't have a context
1089     // then, and this function would correctly return EGL_NO_CONTEXT.
1090
1091     clearError();
1092
1093     EGLContext ctx = getContext();
1094     return ctx;
1095 }
1096
1097 EGLSurface eglGetCurrentSurface(EGLint readdraw)
1098 {
1099     // could be called before eglInitialize(), but we wouldn't have a context
1100     // then, and this function would correctly return EGL_NO_SURFACE.
1101
1102     clearError();
1103
1104     EGLContext ctx = getContext();
1105     if (ctx) {
1106         egl_context_t const * const c = get_context(ctx);
1107         if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1108         switch (readdraw) {
1109             case EGL_READ: return c->read;
1110             case EGL_DRAW: return c->draw;
1111             default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1112         }
1113     }
1114     return EGL_NO_SURFACE;
1115 }
1116
1117 EGLDisplay eglGetCurrentDisplay(void)
1118 {
1119     // could be called before eglInitialize(), but we wouldn't have a context
1120     // then, and this function would correctly return EGL_NO_DISPLAY.
1121
1122     clearError();
1123
1124     EGLContext ctx = getContext();
1125     if (ctx) {
1126         egl_context_t const * const c = get_context(ctx);
1127         if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
1128         return c->dpy;
1129     }
1130     return EGL_NO_DISPLAY;
1131 }
1132
1133 EGLBoolean eglWaitGL(void)
1134 {
1135     clearError();
1136
1137     egl_connection_t* const cnx = &gEGLImpl;
1138     if (!cnx->dso)
1139         return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1140
1141     return cnx->egl.eglWaitGL();
1142 }
1143
1144 EGLBoolean eglWaitNative(EGLint engine)
1145 {
1146     clearError();
1147
1148     egl_connection_t* const cnx = &gEGLImpl;
1149     if (!cnx->dso)
1150         return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1151
1152     return cnx->egl.eglWaitNative(engine);
1153 }
1154
1155 EGLint eglGetError(void)
1156 {
1157     EGLint err = EGL_SUCCESS;
1158     egl_connection_t* const cnx = &gEGLImpl;
1159     if (cnx->dso) {
1160         err = cnx->egl.eglGetError();
1161     }
1162     if (err == EGL_SUCCESS) {
1163         err = egl_tls_t::getError();
1164     }
1165     return err;
1166 }
1167
1168 static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
1169         const char* procname) {
1170     const egl_connection_t* cnx = &gEGLImpl;
1171     void* proc = NULL;
1172
1173     proc = dlsym(cnx->libEgl, procname);
1174     if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1175
1176     proc = dlsym(cnx->libGles2, procname);
1177     if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1178
1179     proc = dlsym(cnx->libGles1, procname);
1180     if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
1181
1182     return NULL;
1183 }
1184
1185 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
1186 {
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.
1190
1191     clearError();
1192
1193     if (egl_init_drivers() == EGL_FALSE) {
1194         setError(EGL_BAD_PARAMETER, NULL);
1195         return  NULL;
1196     }
1197
1198     if (FILTER_EXTENSIONS(procname)) {
1199         return NULL;
1200     }
1201
1202     __eglMustCastToProperFunctionPointerType addr;
1203     addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
1204     if (addr) return addr;
1205
1206     addr = findBuiltinWrapper(procname);
1207     if (addr) return addr;
1208
1209     // this protects accesses to sGLExtentionMap and sGLExtentionSlot
1210     pthread_mutex_lock(&sExtensionMapMutex);
1211
1212         /*
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.
1216          *
1217          * For this reason, we return a "forwarder", a small stub that takes
1218          * care of calling the function associated with the context
1219          * currently bound.
1220          *
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.
1226          *
1227          */
1228
1229         const std::string name(procname);
1230
1231     auto& extentionMap = sGLExtentionMap;
1232     auto pos = extentionMap.find(name);
1233         addr = (pos != extentionMap.end()) ? pos->second : nullptr;
1234         const int slot = sGLExtentionSlot;
1235
1236         ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
1237                 "no more slots for eglGetProcAddress(\"%s\")",
1238                 procname);
1239
1240         if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
1241             bool found = false;
1242
1243             egl_connection_t* const cnx = &gEGLImpl;
1244             if (cnx->dso && cnx->egl.eglGetProcAddress) {
1245                 // Extensions are independent of the bound context
1246                 addr =
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;
1251             }
1252
1253             if (found) {
1254                 addr = gExtensionForwarders[slot];
1255                 extentionMap[name] = addr;
1256                 sGLExtentionSlot++;
1257             }
1258         }
1259
1260     pthread_mutex_unlock(&sExtensionMapMutex);
1261     return addr;
1262 }
1263
1264 class FrameCompletionThread {
1265 public:
1266
1267     static void queueSync(EGLSyncKHR sync) {
1268         static FrameCompletionThread thread;
1269
1270         char name[64];
1271
1272         std::lock_guard<std::mutex> lock(thread.mMutex);
1273         snprintf(name, sizeof(name), "kicked off frame %u", (unsigned int)thread.mFramesQueued);
1274         ATRACE_NAME(name);
1275
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()));
1280     }
1281
1282 private:
1283
1284     FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {
1285         std::thread thread(&FrameCompletionThread::loop, this);
1286         thread.detach();
1287     }
1288
1289 #pragma clang diagnostic push
1290 #pragma clang diagnostic ignored "-Wmissing-noreturn"
1291     void loop() {
1292         while (true) {
1293             threadLoop();
1294         }
1295     }
1296 #pragma clang diagnostic pop
1297
1298     void threadLoop() {
1299         EGLSyncKHR sync;
1300         uint32_t frameNum;
1301         {
1302             std::unique_lock<std::mutex> lock(mMutex);
1303             while (mQueue.empty()) {
1304                 mCondition.wait(lock);
1305             }
1306             sync = mQueue[0];
1307             frameNum = mFramesCompleted;
1308         }
1309         EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1310         {
1311             char name[64];
1312             snprintf(name, sizeof(name), "waiting for frame %u", (unsigned int)frameNum);
1313             ATRACE_NAME(name);
1314
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");
1320             }
1321             eglDestroySyncKHR(dpy, sync);
1322         }
1323         {
1324             std::lock_guard<std::mutex> lock(mMutex);
1325             mQueue.pop_front();
1326             mFramesCompleted++;
1327             ATRACE_INT("GPU Frames Outstanding", int32_t(mQueue.size()));
1328         }
1329     }
1330
1331     uint32_t mFramesQueued;
1332     uint32_t mFramesCompleted;
1333     std::deque<EGLSyncKHR> mQueue;
1334     std::condition_variable mCondition;
1335     std::mutex mMutex;
1336 };
1337
1338 EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1339         EGLint *rects, EGLint n_rects)
1340 {
1341     ATRACE_CALL();
1342     clearError();
1343
1344     const egl_display_ptr dp = validate_display(dpy);
1345     if (!dp) return EGL_FALSE;
1346
1347     SurfaceRef _s(dp.get(), draw);
1348     if (!_s.get())
1349         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1350
1351     egl_surface_t const * const s = get_surface(draw);
1352
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);
1357         }
1358     }
1359
1360     if (CC_UNLIKELY(dp->finishOnSwap)) {
1361         uint32_t pixel;
1362         egl_context_t * const c = get_context( egl_tls_t::getContext() );
1363         if (c) {
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);
1367         }
1368     }
1369
1370     if (n_rects == 0) {
1371         return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1372     }
1373
1374     std::vector<android_native_rect_t> androidRects((size_t)n_rects);
1375     for (int r = 0; r < n_rects; ++r) {
1376         int offset = r * 4;
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);
1387     }
1388     native_window_set_surface_damage(s->getNativeWindow(), androidRects.data(), androidRects.size());
1389
1390     if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1391         return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1392                 rects, n_rects);
1393     } else {
1394         return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1395     }
1396 }
1397
1398 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1399 {
1400     return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
1401 }
1402
1403 EGLBoolean eglCopyBuffers(  EGLDisplay dpy, EGLSurface surface,
1404                             NativePixmapType target)
1405 {
1406     clearError();
1407
1408     const egl_display_ptr dp = validate_display(dpy);
1409     if (!dp) return EGL_FALSE;
1410
1411     SurfaceRef _s(dp.get(), surface);
1412     if (!_s.get())
1413         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1414
1415     egl_surface_t const * const s = get_surface(surface);
1416     return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
1417 }
1418
1419 const char* eglQueryString(EGLDisplay dpy, EGLint name)
1420 {
1421     clearError();
1422
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.
1427     //
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);
1432
1433     const egl_display_ptr dp = validate_display(dpy);
1434     if (!dp) return (const char *) NULL;
1435
1436     switch (name) {
1437         case EGL_VENDOR:
1438             return dp->getVendorString();
1439         case EGL_VERSION:
1440             return dp->getVersionString();
1441         case EGL_EXTENSIONS:
1442             return dp->getExtensionString();
1443         case EGL_CLIENT_APIS:
1444             return dp->getClientApiString();
1445         default:
1446             break;
1447     }
1448     return setError(EGL_BAD_PARAMETER, (const char *)0);
1449 }
1450
1451 extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1452 {
1453     clearError();
1454
1455     const egl_display_ptr dp = validate_display(dpy);
1456     if (!dp) return (const char *) NULL;
1457
1458     switch (name) {
1459         case EGL_VENDOR:
1460             return dp->disp.queryString.vendor;
1461         case EGL_VERSION:
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;
1467         default:
1468             break;
1469     }
1470     return setError(EGL_BAD_PARAMETER, (const char *)0);
1471 }
1472
1473 // ----------------------------------------------------------------------------
1474 // EGL 1.1
1475 // ----------------------------------------------------------------------------
1476
1477 EGLBoolean eglSurfaceAttrib(
1478         EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1479 {
1480     clearError();
1481
1482     const egl_display_ptr dp = validate_display(dpy);
1483     if (!dp) return EGL_FALSE;
1484
1485     SurfaceRef _s(dp.get(), surface);
1486     if (!_s.get())
1487         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1488
1489     egl_surface_t * const s = get_surface(surface);
1490
1491     if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
1492         if (!s->getNativeWindow()) {
1493             setError(EGL_BAD_SURFACE, EGL_FALSE);
1494         }
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);
1497     }
1498
1499     if (attribute == EGL_TIMESTAMPS_ANDROID) {
1500         if (!s->getNativeWindow()) {
1501             return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1502         }
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);
1505     }
1506
1507     if (s->cnx->egl.eglSurfaceAttrib) {
1508         return s->cnx->egl.eglSurfaceAttrib(
1509                 dp->disp.dpy, s->surface, attribute, value);
1510     }
1511     return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1512 }
1513
1514 EGLBoolean eglBindTexImage(
1515         EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1516 {
1517     clearError();
1518
1519     const egl_display_ptr dp = validate_display(dpy);
1520     if (!dp) return EGL_FALSE;
1521
1522     SurfaceRef _s(dp.get(), surface);
1523     if (!_s.get())
1524         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1525
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);
1530     }
1531     return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1532 }
1533
1534 EGLBoolean eglReleaseTexImage(
1535         EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1536 {
1537     clearError();
1538
1539     const egl_display_ptr dp = validate_display(dpy);
1540     if (!dp) return EGL_FALSE;
1541
1542     SurfaceRef _s(dp.get(), surface);
1543     if (!_s.get())
1544         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1545
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);
1550     }
1551     return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1552 }
1553
1554 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1555 {
1556     clearError();
1557
1558     const egl_display_ptr dp = validate_display(dpy);
1559     if (!dp) return EGL_FALSE;
1560
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);
1565     }
1566
1567     return res;
1568 }
1569
1570
1571 // ----------------------------------------------------------------------------
1572 // EGL 1.2
1573 // ----------------------------------------------------------------------------
1574
1575 EGLBoolean eglWaitClient(void)
1576 {
1577     clearError();
1578
1579     egl_connection_t* const cnx = &gEGLImpl;
1580     if (!cnx->dso)
1581         return setError(EGL_BAD_CONTEXT, (EGLBoolean)EGL_FALSE);
1582
1583     EGLBoolean res;
1584     if (cnx->egl.eglWaitClient) {
1585         res = cnx->egl.eglWaitClient();
1586     } else {
1587         res = cnx->egl.eglWaitGL();
1588     }
1589     return res;
1590 }
1591
1592 EGLBoolean eglBindAPI(EGLenum api)
1593 {
1594     clearError();
1595
1596     if (egl_init_drivers() == EGL_FALSE) {
1597         return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
1598     }
1599
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);
1605     }
1606     return res;
1607 }
1608
1609 EGLenum eglQueryAPI(void)
1610 {
1611     clearError();
1612
1613     if (egl_init_drivers() == EGL_FALSE) {
1614         return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
1615     }
1616
1617     egl_connection_t* const cnx = &gEGLImpl;
1618     if (cnx->dso && cnx->egl.eglQueryAPI) {
1619         return cnx->egl.eglQueryAPI();
1620     }
1621
1622     // or, it can only be OpenGL ES
1623     return EGL_OPENGL_ES_API;
1624 }
1625
1626 EGLBoolean eglReleaseThread(void)
1627 {
1628     clearError();
1629
1630     egl_connection_t* const cnx = &gEGLImpl;
1631     if (cnx->dso && cnx->egl.eglReleaseThread) {
1632         cnx->egl.eglReleaseThread();
1633     }
1634
1635     // If there is context bound to the thread, release it
1636     egl_display_t::loseCurrent(get_context(getContext()));
1637
1638     egl_tls_t::clearTLS();
1639     return EGL_TRUE;
1640 }
1641
1642 EGLSurface eglCreatePbufferFromClientBuffer(
1643           EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1644           EGLConfig config, const EGLint *attrib_list)
1645 {
1646     clearError();
1647
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);
1654     }
1655     return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1656 }
1657
1658 // ----------------------------------------------------------------------------
1659 // EGL_EGLEXT_VERSION 3
1660 // ----------------------------------------------------------------------------
1661
1662 EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1663         const EGLint *attrib_list)
1664 {
1665     clearError();
1666
1667     const egl_display_ptr dp = validate_display(dpy);
1668     if (!dp) return EGL_FALSE;
1669
1670     SurfaceRef _s(dp.get(), surface);
1671     if (!_s.get())
1672         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1673
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);
1678     }
1679     return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
1680 }
1681
1682 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1683 {
1684     clearError();
1685
1686     const egl_display_ptr dp = validate_display(dpy);
1687     if (!dp) return EGL_FALSE;
1688
1689     SurfaceRef _s(dp.get(), surface);
1690     if (!_s.get())
1691         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
1692
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);
1696     }
1697     return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
1698 }
1699
1700 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1701         EGLClientBuffer buffer, const EGLint *attrib_list)
1702 {
1703     clearError();
1704
1705     const egl_display_ptr dp = validate_display(dpy);
1706     if (!dp) return EGL_NO_IMAGE_KHR;
1707
1708     ContextRef _c(dp.get(), ctx);
1709     egl_context_t * const c = _c.get();
1710
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(
1715                 dp->disp.dpy,
1716                 c ? c->context : EGL_NO_CONTEXT,
1717                 target, buffer, attrib_list);
1718     }
1719     return result;
1720 }
1721
1722 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1723 {
1724     clearError();
1725
1726     const egl_display_ptr dp = validate_display(dpy);
1727     if (!dp) return EGL_FALSE;
1728
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);
1733     }
1734     return result;
1735 }
1736
1737 // ----------------------------------------------------------------------------
1738 // EGL_EGLEXT_VERSION 5
1739 // ----------------------------------------------------------------------------
1740
1741
1742 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1743 {
1744     clearError();
1745
1746     const egl_display_ptr dp = validate_display(dpy);
1747     if (!dp) return EGL_NO_SYNC_KHR;
1748
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);
1753     }
1754     return result;
1755 }
1756
1757 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1758 {
1759     clearError();
1760
1761     const egl_display_ptr dp = validate_display(dpy);
1762     if (!dp) return EGL_FALSE;
1763
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);
1768     }
1769     return result;
1770 }
1771
1772 EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1773     clearError();
1774
1775     const egl_display_ptr dp = validate_display(dpy);
1776     if (!dp) return EGL_FALSE;
1777
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);
1783     }
1784     return result;
1785 }
1786
1787 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1788         EGLint flags, EGLTimeKHR timeout)
1789 {
1790     clearError();
1791
1792     const egl_display_ptr dp = validate_display(dpy);
1793     if (!dp) return EGL_FALSE;
1794
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);
1800     }
1801     return result;
1802 }
1803
1804 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1805         EGLint attribute, EGLint *value)
1806 {
1807     clearError();
1808
1809     const egl_display_ptr dp = validate_display(dpy);
1810     if (!dp) return EGL_FALSE;
1811
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);
1817     }
1818     return result;
1819 }
1820
1821 EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1822 {
1823     clearError();
1824
1825     const egl_display_ptr dp = validate_display(dpy);
1826     if (!dp) return EGL_NO_STREAM_KHR;
1827
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);
1833     }
1834     return result;
1835 }
1836
1837 EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1838 {
1839     clearError();
1840
1841     const egl_display_ptr dp = validate_display(dpy);
1842     if (!dp) return EGL_FALSE;
1843
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);
1849     }
1850     return result;
1851 }
1852
1853 EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1854         EGLenum attribute, EGLint value)
1855 {
1856     clearError();
1857
1858     const egl_display_ptr dp = validate_display(dpy);
1859     if (!dp) return EGL_FALSE;
1860
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);
1866     }
1867     return result;
1868 }
1869
1870 EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1871         EGLenum attribute, EGLint *value)
1872 {
1873     clearError();
1874
1875     const egl_display_ptr dp = validate_display(dpy);
1876     if (!dp) return EGL_FALSE;
1877
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);
1883     }
1884     return result;
1885 }
1886
1887 EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1888         EGLenum attribute, EGLuint64KHR *value)
1889 {
1890     clearError();
1891
1892     const egl_display_ptr dp = validate_display(dpy);
1893     if (!dp) return EGL_FALSE;
1894
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);
1900     }
1901     return result;
1902 }
1903
1904 EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1905         EGLenum attribute, EGLTimeKHR *value)
1906 {
1907     clearError();
1908
1909     const egl_display_ptr dp = validate_display(dpy);
1910     if (!dp) return EGL_FALSE;
1911
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);
1917     }
1918     return result;
1919 }
1920
1921 EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1922         EGLStreamKHR stream, const EGLint *attrib_list)
1923 {
1924     clearError();
1925
1926     egl_display_ptr dp = validate_display(dpy);
1927     if (!dp) return EGL_NO_SURFACE;
1928
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);
1937     }
1938
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);
1945             return s;
1946         }
1947     }
1948     return EGL_NO_SURFACE;
1949 }
1950
1951 EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1952         EGLStreamKHR stream)
1953 {
1954     clearError();
1955
1956     const egl_display_ptr dp = validate_display(dpy);
1957     if (!dp) return EGL_FALSE;
1958
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);
1964     }
1965     return result;
1966 }
1967
1968 EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1969         EGLStreamKHR stream)
1970 {
1971     clearError();
1972
1973     const egl_display_ptr dp = validate_display(dpy);
1974     if (!dp) return EGL_FALSE;
1975
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);
1981     }
1982     return result;
1983 }
1984
1985 EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1986         EGLStreamKHR stream)
1987 {
1988     clearError();
1989
1990     const egl_display_ptr dp = validate_display(dpy);
1991     if (!dp) return EGL_FALSE;
1992
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);
1998     }
1999     return result;
2000 }
2001
2002 EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
2003         EGLDisplay dpy, EGLStreamKHR stream)
2004 {
2005     clearError();
2006
2007     const egl_display_ptr dp = validate_display(dpy);
2008     if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
2009
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);
2015     }
2016     return result;
2017 }
2018
2019 EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
2020         EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
2021 {
2022     clearError();
2023
2024     const egl_display_ptr dp = validate_display(dpy);
2025     if (!dp) return EGL_NO_STREAM_KHR;
2026
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);
2032     }
2033     return result;
2034 }
2035
2036 // ----------------------------------------------------------------------------
2037 // EGL_EGLEXT_VERSION 15
2038 // ----------------------------------------------------------------------------
2039
2040 EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
2041     clearError();
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);
2048     }
2049     return result;
2050 }
2051
2052 // ----------------------------------------------------------------------------
2053 // ANDROID extensions
2054 // ----------------------------------------------------------------------------
2055
2056 EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
2057 {
2058     clearError();
2059
2060     const egl_display_ptr dp = validate_display(dpy);
2061     if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
2062
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);
2067     }
2068     return result;
2069 }
2070
2071 EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
2072         EGLnsecsANDROID time)
2073 {
2074     clearError();
2075
2076     const egl_display_ptr dp = validate_display(dpy);
2077     if (!dp) {
2078         return EGL_FALSE;
2079     }
2080
2081     SurfaceRef _s(dp.get(), surface);
2082     if (!_s.get()) {
2083         setError(EGL_BAD_SURFACE, EGL_FALSE);
2084         return EGL_FALSE;
2085     }
2086
2087     egl_surface_t const * const s = get_surface(surface);
2088     native_window_set_buffers_timestamp(s->getNativeWindow(), time);
2089
2090     return EGL_TRUE;
2091 }
2092
2093 EGLClientBuffer eglGetNativeClientBufferANDROID(const AHardwareBuffer *buffer) {
2094     clearError();
2095     // AHardwareBuffer_to_ANativeWindowBuffer is a platform-only symbol and thus
2096     // this function cannot be implemented when this libEGL is built for
2097     // vendors.
2098 #ifndef __ANDROID_VNDK__
2099     if (!buffer) return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2100     return const_cast<ANativeWindowBuffer *>(AHardwareBuffer_to_ANativeWindowBuffer(buffer));
2101 #else
2102     return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
2103 #endif
2104 }
2105
2106 // ----------------------------------------------------------------------------
2107 // NVIDIA extensions
2108 // ----------------------------------------------------------------------------
2109 EGLuint64NV eglGetSystemTimeFrequencyNV()
2110 {
2111     clearError();
2112
2113     if (egl_init_drivers() == EGL_FALSE) {
2114         return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
2115     }
2116
2117     EGLuint64NV ret = 0;
2118     egl_connection_t* const cnx = &gEGLImpl;
2119
2120     if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
2121         return cnx->egl.eglGetSystemTimeFrequencyNV();
2122     }
2123
2124     return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
2125 }
2126
2127 EGLuint64NV eglGetSystemTimeNV()
2128 {
2129     clearError();
2130
2131     if (egl_init_drivers() == EGL_FALSE) {
2132         return setError(EGL_BAD_PARAMETER, (EGLuint64NV)EGL_FALSE);
2133     }
2134
2135     EGLuint64NV ret = 0;
2136     egl_connection_t* const cnx = &gEGLImpl;
2137
2138     if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
2139         return cnx->egl.eglGetSystemTimeNV();
2140     }
2141
2142     return setErrorQuiet(EGL_BAD_DISPLAY, (EGLuint64NV)0);
2143 }
2144
2145 // ----------------------------------------------------------------------------
2146 // Partial update extension
2147 // ----------------------------------------------------------------------------
2148 EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
2149         EGLint *rects, EGLint n_rects)
2150 {
2151     clearError();
2152
2153     const egl_display_ptr dp = validate_display(dpy);
2154     if (!dp) {
2155         setError(EGL_BAD_DISPLAY, EGL_FALSE);
2156         return EGL_FALSE;
2157     }
2158
2159     SurfaceRef _s(dp.get(), surface);
2160     if (!_s.get()) {
2161         setError(EGL_BAD_SURFACE, EGL_FALSE);
2162         return EGL_FALSE;
2163     }
2164
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,
2168                 rects, n_rects);
2169     }
2170
2171     return EGL_FALSE;
2172 }
2173
2174 EGLBoolean eglGetNextFrameIdANDROID(EGLDisplay dpy, EGLSurface surface,
2175             EGLuint64KHR *frameId) {
2176     clearError();
2177
2178     const egl_display_ptr dp = validate_display(dpy);
2179     if (!dp) {
2180         return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2181     }
2182
2183     SurfaceRef _s(dp.get(), surface);
2184     if (!_s.get()) {
2185         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2186     }
2187
2188     egl_surface_t const * const s = get_surface(surface);
2189
2190     if (!s->getNativeWindow()) {
2191         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2192     }
2193
2194     uint64_t nextFrameId = 0;
2195     int ret = native_window_get_next_frame_id(s->getNativeWindow(), &nextFrameId);
2196
2197     if (ret != 0) {
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);
2202     }
2203
2204     *frameId = nextFrameId;
2205     return EGL_TRUE;
2206 }
2207
2208 EGLBoolean eglGetCompositorTimingANDROID(EGLDisplay dpy, EGLSurface surface,
2209         EGLint numTimestamps, const EGLint *names, EGLnsecsANDROID *values)
2210 {
2211     clearError();
2212
2213     const egl_display_ptr dp = validate_display(dpy);
2214     if (!dp) {
2215         return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2216     }
2217
2218     SurfaceRef _s(dp.get(), surface);
2219     if (!_s.get()) {
2220         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2221     }
2222
2223     egl_surface_t const * const s = get_surface(surface);
2224
2225     if (!s->getNativeWindow()) {
2226         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2227     }
2228
2229     nsecs_t* compositeDeadline = nullptr;
2230     nsecs_t* compositeInterval = nullptr;
2231     nsecs_t* compositeToPresentLatency = nullptr;
2232
2233     for (int i = 0; i < numTimestamps; i++) {
2234         switch (names[i]) {
2235             case EGL_COMPOSITE_DEADLINE_ANDROID:
2236                 compositeDeadline = &values[i];
2237                 break;
2238             case EGL_COMPOSITE_INTERVAL_ANDROID:
2239                 compositeInterval = &values[i];
2240                 break;
2241             case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2242                 compositeToPresentLatency = &values[i];
2243                 break;
2244             default:
2245                 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2246         }
2247     }
2248
2249     int ret = native_window_get_compositor_timing(s->getNativeWindow(),
2250             compositeDeadline, compositeInterval, compositeToPresentLatency);
2251
2252     switch (ret) {
2253       case 0:
2254         return EGL_TRUE;
2255       case -ENOSYS:
2256         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2257       default:
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);
2262     }
2263 }
2264
2265 EGLBoolean eglGetCompositorTimingSupportedANDROID(
2266         EGLDisplay dpy, EGLSurface surface, EGLint name)
2267 {
2268     clearError();
2269
2270     const egl_display_ptr dp = validate_display(dpy);
2271     if (!dp) {
2272         return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2273     }
2274
2275     SurfaceRef _s(dp.get(), surface);
2276     if (!_s.get()) {
2277         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2278     }
2279
2280     egl_surface_t const * const s = get_surface(surface);
2281
2282     ANativeWindow* window = s->getNativeWindow();
2283     if (!window) {
2284         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2285     }
2286
2287     switch (name) {
2288         case EGL_COMPOSITE_DEADLINE_ANDROID:
2289         case EGL_COMPOSITE_INTERVAL_ANDROID:
2290         case EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID:
2291             return EGL_TRUE;
2292         default:
2293             return EGL_FALSE;
2294     }
2295 }
2296
2297 EGLBoolean eglGetFrameTimestampsANDROID(EGLDisplay dpy, EGLSurface surface,
2298         EGLuint64KHR frameId, EGLint numTimestamps, const EGLint *timestamps,
2299         EGLnsecsANDROID *values)
2300 {
2301     clearError();
2302
2303     const egl_display_ptr dp = validate_display(dpy);
2304     if (!dp) {
2305         return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2306     }
2307
2308     SurfaceRef _s(dp.get(), surface);
2309     if (!_s.get()) {
2310         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2311     }
2312
2313     egl_surface_t const * const s = get_surface(surface);
2314
2315     if (!s->getNativeWindow()) {
2316         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2317     }
2318
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;
2328
2329     for (int i = 0; i < numTimestamps; i++) {
2330         switch (timestamps[i]) {
2331             case EGL_REQUESTED_PRESENT_TIME_ANDROID:
2332                 requestedPresentTime = &values[i];
2333                 break;
2334             case EGL_RENDERING_COMPLETE_TIME_ANDROID:
2335                 acquireTime = &values[i];
2336                 break;
2337             case EGL_COMPOSITION_LATCH_TIME_ANDROID:
2338                 latchTime = &values[i];
2339                 break;
2340             case EGL_FIRST_COMPOSITION_START_TIME_ANDROID:
2341                 firstRefreshStartTime = &values[i];
2342                 break;
2343             case EGL_LAST_COMPOSITION_START_TIME_ANDROID:
2344                 lastRefreshStartTime = &values[i];
2345                 break;
2346             case EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID:
2347                 gpuCompositionDoneTime = &values[i];
2348                 break;
2349             case EGL_DISPLAY_PRESENT_TIME_ANDROID:
2350                 displayPresentTime = &values[i];
2351                 break;
2352             case EGL_DEQUEUE_READY_TIME_ANDROID:
2353                 dequeueReadyTime = &values[i];
2354                 break;
2355             case EGL_READS_DONE_TIME_ANDROID:
2356                 releaseTime = &values[i];
2357                 break;
2358             default:
2359                 return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2360         }
2361     }
2362
2363     int ret = native_window_get_frame_timestamps(s->getNativeWindow(), frameId,
2364             requestedPresentTime, acquireTime, latchTime, firstRefreshStartTime,
2365             lastRefreshStartTime, gpuCompositionDoneTime, displayPresentTime,
2366             dequeueReadyTime, releaseTime);
2367
2368     switch (ret) {
2369         case 0:
2370             return EGL_TRUE;
2371         case -ENOENT:
2372             return setError(EGL_BAD_ACCESS, (EGLBoolean)EGL_FALSE);
2373         case -ENOSYS:
2374             return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2375         case -EINVAL:
2376             return setError(EGL_BAD_PARAMETER, (EGLBoolean)EGL_FALSE);
2377         default:
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);
2382     }
2383 }
2384
2385 EGLBoolean eglGetFrameTimestampSupportedANDROID(
2386         EGLDisplay dpy, EGLSurface surface, EGLint timestamp)
2387 {
2388     clearError();
2389
2390     const egl_display_ptr dp = validate_display(dpy);
2391     if (!dp) {
2392         return setError(EGL_BAD_DISPLAY, (EGLBoolean)EGL_FALSE);
2393     }
2394
2395     SurfaceRef _s(dp.get(), surface);
2396     if (!_s.get()) {
2397         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2398     }
2399
2400     egl_surface_t const * const s = get_surface(surface);
2401
2402     ANativeWindow* window = s->getNativeWindow();
2403     if (!window) {
2404         return setError(EGL_BAD_SURFACE, (EGLBoolean)EGL_FALSE);
2405     }
2406
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:
2419             return EGL_TRUE;
2420         case EGL_DISPLAY_PRESENT_TIME_ANDROID: {
2421             int value = 0;
2422             window->query(window,
2423                     NATIVE_WINDOW_FRAME_TIMESTAMPS_SUPPORTS_PRESENT, &value);
2424             return value == 0 ? EGL_FALSE : EGL_TRUE;
2425         }
2426         default:
2427             return EGL_FALSE;
2428     }
2429 }