OSDN Git Service

Revert "EGL: Ensure surfaces are disconnected when destroyed"
[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 <dlfcn.h>
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <hardware/gralloc.h>
25 #include <system/window.h>
26
27 #include <EGL/egl.h>
28 #include <EGL/eglext.h>
29
30 #include <cutils/log.h>
31 #include <cutils/atomic.h>
32 #include <cutils/compiler.h>
33 #include <cutils/properties.h>
34 #include <cutils/memory.h>
35
36 #include <ui/GraphicBuffer.h>
37
38 #include <utils/KeyedVector.h>
39 #include <utils/SortedVector.h>
40 #include <utils/String8.h>
41 #include <utils/Trace.h>
42
43 #include "../egl_impl.h"
44 #include "../hooks.h"
45
46 #include "egl_display.h"
47 #include "egl_object.h"
48 #include "egl_tls.h"
49 #include "egldefs.h"
50
51 using namespace android;
52
53 // This extension has not been ratified yet, so can't be shipped.
54 // Implementation is incomplete and untested.
55 #define ENABLE_EGL_KHR_GL_COLORSPACE 0
56
57 // ----------------------------------------------------------------------------
58
59 namespace android {
60
61 struct extention_map_t {
62     const char* name;
63     __eglMustCastToProperFunctionPointerType address;
64 };
65
66 /*
67  * This is the list of EGL extensions exposed to applications.
68  *
69  * Some of them (gBuiltinExtensionString) are implemented entirely in this EGL
70  * wrapper and are always available.
71  *
72  * The rest (gExtensionString) depend on support in the EGL driver, and are
73  * only available if the driver supports them. However, some of these must be
74  * supported because they are used by the Android system itself; these are
75  * listed as mandatory below and are required by the CDD. The system *assumes*
76  * the mandatory extensions are present and may not function properly if some
77  * are missing.
78  *
79  * NOTE: Both strings MUST have a single space as the last character.
80  */
81 extern char const * const gBuiltinExtensionString =
82         "EGL_KHR_get_all_proc_addresses "
83         "EGL_ANDROID_presentation_time "
84         "EGL_KHR_swap_buffers_with_damage "
85         "EGL_ANDROID_create_native_client_buffer "
86         "EGL_ANDROID_front_buffer_auto_refresh "
87         ;
88 extern char const * const gExtensionString  =
89         "EGL_KHR_image "                        // mandatory
90         "EGL_KHR_image_base "                   // mandatory
91         "EGL_KHR_image_pixmap "
92         "EGL_KHR_lock_surface "
93 #if (ENABLE_EGL_KHR_GL_COLORSPACE != 0)
94         "EGL_KHR_gl_colorspace "
95 #endif
96         "EGL_KHR_gl_texture_2D_image "
97         "EGL_KHR_gl_texture_3D_image "
98         "EGL_KHR_gl_texture_cubemap_image "
99         "EGL_KHR_gl_renderbuffer_image "
100         "EGL_KHR_reusable_sync "
101         "EGL_KHR_fence_sync "
102         "EGL_KHR_create_context "
103         "EGL_KHR_config_attribs "
104         "EGL_KHR_surfaceless_context "
105         "EGL_KHR_stream "
106         "EGL_KHR_stream_fifo "
107         "EGL_KHR_stream_producer_eglsurface "
108         "EGL_KHR_stream_consumer_gltexture "
109         "EGL_KHR_stream_cross_process_fd "
110         "EGL_EXT_create_context_robustness "
111         "EGL_NV_system_time "
112         "EGL_ANDROID_image_native_buffer "      // mandatory
113         "EGL_KHR_wait_sync "                    // strongly recommended
114         "EGL_ANDROID_recordable "               // mandatory
115         "EGL_KHR_partial_update "               // strongly recommended
116         "EGL_EXT_buffer_age "                   // strongly recommended with partial_update
117         "EGL_KHR_create_context_no_error "
118         "EGL_KHR_mutable_render_buffer "
119         "EGL_EXT_yuv_surface "
120         ;
121
122 // extensions not exposed to applications but used by the ANDROID system
123 //      "EGL_ANDROID_blob_cache "               // strongly recommended
124 //      "EGL_IMG_hibernate_process "            // optional
125 //      "EGL_ANDROID_native_fence_sync "        // strongly recommended
126 //      "EGL_ANDROID_framebuffer_target "       // mandatory for HWC 1.1
127 //      "EGL_ANDROID_image_crop "               // optional
128
129 /*
130  * EGL Extensions entry-points exposed to 3rd party applications
131  * (keep in sync with gExtensionString above)
132  *
133  */
134 static const extention_map_t sExtensionMap[] = {
135     // EGL_KHR_lock_surface
136     { "eglLockSurfaceKHR",
137             (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR },
138     { "eglUnlockSurfaceKHR",
139             (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR },
140
141     // EGL_KHR_image, EGL_KHR_image_base
142     { "eglCreateImageKHR",
143             (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR },
144     { "eglDestroyImageKHR",
145             (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR },
146
147     // EGL_KHR_reusable_sync, EGL_KHR_fence_sync
148     { "eglCreateSyncKHR",
149             (__eglMustCastToProperFunctionPointerType)&eglCreateSyncKHR },
150     { "eglDestroySyncKHR",
151             (__eglMustCastToProperFunctionPointerType)&eglDestroySyncKHR },
152     { "eglClientWaitSyncKHR",
153             (__eglMustCastToProperFunctionPointerType)&eglClientWaitSyncKHR },
154     { "eglSignalSyncKHR",
155             (__eglMustCastToProperFunctionPointerType)&eglSignalSyncKHR },
156     { "eglGetSyncAttribKHR",
157             (__eglMustCastToProperFunctionPointerType)&eglGetSyncAttribKHR },
158
159     // EGL_NV_system_time
160     { "eglGetSystemTimeFrequencyNV",
161             (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV },
162     { "eglGetSystemTimeNV",
163             (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV },
164
165     // EGL_KHR_wait_sync
166     { "eglWaitSyncKHR",
167             (__eglMustCastToProperFunctionPointerType)&eglWaitSyncKHR },
168
169     // EGL_ANDROID_presentation_time
170     { "eglPresentationTimeANDROID",
171             (__eglMustCastToProperFunctionPointerType)&eglPresentationTimeANDROID },
172
173     // EGL_KHR_swap_buffers_with_damage
174     { "eglSwapBuffersWithDamageKHR",
175             (__eglMustCastToProperFunctionPointerType)&eglSwapBuffersWithDamageKHR },
176
177     // EGL_ANDROID_native_client_buffer
178     { "eglCreateNativeClientBufferANDROID",
179             (__eglMustCastToProperFunctionPointerType)&eglCreateNativeClientBufferANDROID },
180
181     // EGL_KHR_partial_update
182     { "eglSetDamageRegionKHR",
183             (__eglMustCastToProperFunctionPointerType)&eglSetDamageRegionKHR },
184
185     { "eglCreateStreamKHR",
186             (__eglMustCastToProperFunctionPointerType)&eglCreateStreamKHR },
187     { "eglDestroyStreamKHR",
188             (__eglMustCastToProperFunctionPointerType)&eglDestroyStreamKHR },
189     { "eglStreamAttribKHR",
190             (__eglMustCastToProperFunctionPointerType)&eglStreamAttribKHR },
191     { "eglQueryStreamKHR",
192             (__eglMustCastToProperFunctionPointerType)&eglQueryStreamKHR },
193     { "eglQueryStreamu64KHR",
194             (__eglMustCastToProperFunctionPointerType)&eglQueryStreamu64KHR },
195     { "eglQueryStreamTimeKHR",
196             (__eglMustCastToProperFunctionPointerType)&eglQueryStreamTimeKHR },
197     { "eglCreateStreamProducerSurfaceKHR",
198             (__eglMustCastToProperFunctionPointerType)&eglCreateStreamProducerSurfaceKHR },
199     { "eglStreamConsumerGLTextureExternalKHR",
200             (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerGLTextureExternalKHR },
201     { "eglStreamConsumerAcquireKHR",
202             (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerAcquireKHR },
203     { "eglStreamConsumerReleaseKHR",
204             (__eglMustCastToProperFunctionPointerType)&eglStreamConsumerReleaseKHR },
205     { "eglGetStreamFileDescriptorKHR",
206             (__eglMustCastToProperFunctionPointerType)&eglGetStreamFileDescriptorKHR },
207     { "eglCreateStreamFromFileDescriptorKHR",
208             (__eglMustCastToProperFunctionPointerType)&eglCreateStreamFromFileDescriptorKHR },
209 };
210
211 /*
212  * These extensions entry-points should not be exposed to applications.
213  * They're used internally by the Android EGL layer.
214  */
215 #define FILTER_EXTENSIONS(procname) \
216         (!strcmp((procname), "eglSetBlobCacheFuncsANDROID") ||    \
217          !strcmp((procname), "eglHibernateProcessIMG")      ||    \
218          !strcmp((procname), "eglAwakenProcessIMG")         ||    \
219          !strcmp((procname), "eglDupNativeFenceFDANDROID"))
220
221
222
223 // accesses protected by sExtensionMapMutex
224 static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap;
225 static int sGLExtentionSlot = 0;
226 static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER;
227
228 static void(*findProcAddress(const char* name,
229         const extention_map_t* map, size_t n))() {
230     for (uint32_t i=0 ; i<n ; i++) {
231         if (!strcmp(name, map[i].name)) {
232             return map[i].address;
233         }
234     }
235     return NULL;
236 }
237
238 // ----------------------------------------------------------------------------
239
240 extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
241 extern EGLBoolean egl_init_drivers();
242 extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS];
243 extern gl_hooks_t gHooksTrace;
244
245 } // namespace android;
246
247
248 // ----------------------------------------------------------------------------
249
250 static inline void clearError() { egl_tls_t::clearError(); }
251 static inline EGLContext getContext() { return egl_tls_t::getContext(); }
252
253 // ----------------------------------------------------------------------------
254
255 EGLDisplay eglGetDisplay(EGLNativeDisplayType display)
256 {
257     clearError();
258
259     uintptr_t index = reinterpret_cast<uintptr_t>(display);
260     if (index >= NUM_DISPLAYS) {
261         return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
262     }
263
264     if (egl_init_drivers() == EGL_FALSE) {
265         return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY);
266     }
267
268     EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display);
269     return dpy;
270 }
271
272 // ----------------------------------------------------------------------------
273 // Initialization
274 // ----------------------------------------------------------------------------
275
276 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
277 {
278     clearError();
279
280     egl_display_ptr dp = get_display(dpy);
281     if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
282
283     EGLBoolean res = dp->initialize(major, minor);
284
285     return res;
286 }
287
288 EGLBoolean eglTerminate(EGLDisplay dpy)
289 {
290     // NOTE: don't unload the drivers b/c some APIs can be called
291     // after eglTerminate() has been called. eglTerminate() only
292     // terminates an EGLDisplay, not a EGL itself.
293
294     clearError();
295
296     egl_display_ptr dp = get_display(dpy);
297     if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
298
299     EGLBoolean res = dp->terminate();
300
301     return res;
302 }
303
304 // ----------------------------------------------------------------------------
305 // configuration
306 // ----------------------------------------------------------------------------
307
308 EGLBoolean eglGetConfigs(   EGLDisplay dpy,
309                             EGLConfig *configs,
310                             EGLint config_size, EGLint *num_config)
311 {
312     clearError();
313
314     const egl_display_ptr dp = validate_display(dpy);
315     if (!dp) return EGL_FALSE;
316
317     if (num_config==0) {
318         return setError(EGL_BAD_PARAMETER, EGL_FALSE);
319     }
320
321     EGLBoolean res = EGL_FALSE;
322     *num_config = 0;
323
324     egl_connection_t* const cnx = &gEGLImpl;
325     if (cnx->dso) {
326         res = cnx->egl.eglGetConfigs(
327                 dp->disp.dpy, configs, config_size, num_config);
328     }
329
330     return res;
331 }
332
333 EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list,
334                             EGLConfig *configs, EGLint config_size,
335                             EGLint *num_config)
336 {
337     clearError();
338
339     const egl_display_ptr dp = validate_display(dpy);
340     if (!dp) return EGL_FALSE;
341
342     if (num_config==0) {
343         return setError(EGL_BAD_PARAMETER, EGL_FALSE);
344     }
345
346     EGLBoolean res = EGL_FALSE;
347     *num_config = 0;
348
349     egl_connection_t* const cnx = &gEGLImpl;
350     if (cnx->dso) {
351         if (attrib_list) {
352             char value[PROPERTY_VALUE_MAX];
353             property_get("debug.egl.force_msaa", value, "false");
354
355             if (!strcmp(value, "true")) {
356                 size_t attribCount = 0;
357                 EGLint attrib = attrib_list[0];
358
359                 // Only enable MSAA if the context is OpenGL ES 2.0 and
360                 // if no caveat is requested
361                 const EGLint *attribRendererable = NULL;
362                 const EGLint *attribCaveat = NULL;
363
364                 // Count the number of attributes and look for
365                 // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT
366                 while (attrib != EGL_NONE) {
367                     attrib = attrib_list[attribCount];
368                     switch (attrib) {
369                         case EGL_RENDERABLE_TYPE:
370                             attribRendererable = &attrib_list[attribCount];
371                             break;
372                         case EGL_CONFIG_CAVEAT:
373                             attribCaveat = &attrib_list[attribCount];
374                             break;
375                     }
376                     attribCount++;
377                 }
378
379                 if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT &&
380                         (!attribCaveat || attribCaveat[1] != EGL_NONE)) {
381
382                     // Insert 2 extra attributes to force-enable MSAA 4x
383                     EGLint aaAttribs[attribCount + 4];
384                     aaAttribs[0] = EGL_SAMPLE_BUFFERS;
385                     aaAttribs[1] = 1;
386                     aaAttribs[2] = EGL_SAMPLES;
387                     aaAttribs[3] = 4;
388
389                     memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint));
390
391                     EGLint numConfigAA;
392                     EGLBoolean resAA = cnx->egl.eglChooseConfig(
393                             dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA);
394
395                     if (resAA == EGL_TRUE && numConfigAA > 0) {
396                         ALOGD("Enabling MSAA 4x");
397                         *num_config = numConfigAA;
398                         return resAA;
399                     }
400                 }
401             }
402         }
403
404         res = cnx->egl.eglChooseConfig(
405                 dp->disp.dpy, attrib_list, configs, config_size, num_config);
406     }
407     return res;
408 }
409
410 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
411         EGLint attribute, EGLint *value)
412 {
413     clearError();
414
415     egl_connection_t* cnx = NULL;
416     const egl_display_ptr dp = validate_display_connection(dpy, cnx);
417     if (!dp) return EGL_FALSE;
418
419     return cnx->egl.eglGetConfigAttrib(
420             dp->disp.dpy, config, attribute, value);
421 }
422
423 // ----------------------------------------------------------------------------
424 // surfaces
425 // ----------------------------------------------------------------------------
426
427 // The EGL_KHR_gl_colorspace spec hasn't been ratified yet, so these haven't
428 // been added to the Khronos egl.h.
429 #define EGL_GL_COLORSPACE_KHR           EGL_VG_COLORSPACE
430 #define EGL_GL_COLORSPACE_SRGB_KHR      EGL_VG_COLORSPACE_sRGB
431 #define EGL_GL_COLORSPACE_LINEAR_KHR    EGL_VG_COLORSPACE_LINEAR
432
433 // Turn linear formats into corresponding sRGB formats when colorspace is
434 // EGL_GL_COLORSPACE_SRGB_KHR, or turn sRGB formats into corresponding linear
435 // formats when colorspace is EGL_GL_COLORSPACE_LINEAR_KHR. In any cases where
436 // the modification isn't possible, the original dataSpace is returned.
437 static android_dataspace modifyBufferDataspace( android_dataspace dataSpace,
438                                                 EGLint colorspace) {
439     if (colorspace == EGL_GL_COLORSPACE_LINEAR_KHR) {
440         return HAL_DATASPACE_SRGB_LINEAR;
441     } else if (colorspace == EGL_GL_COLORSPACE_SRGB_KHR) {
442         return HAL_DATASPACE_SRGB;
443     }
444     return dataSpace;
445 }
446
447 EGLSurface eglCreateWindowSurface(  EGLDisplay dpy, EGLConfig config,
448                                     NativeWindowType window,
449                                     const EGLint *attrib_list)
450 {
451     clearError();
452
453     egl_connection_t* cnx = NULL;
454     egl_display_ptr dp = validate_display_connection(dpy, cnx);
455     if (dp) {
456         EGLDisplay iDpy = dp->disp.dpy;
457
458         int result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
459         if (result != OK) {
460             ALOGE("eglCreateWindowSurface: native_window_api_connect (win=%p) "
461                     "failed (%#x) (already connected to another API?)",
462                     window, result);
463             return setError(EGL_BAD_ALLOC, EGL_NO_SURFACE);
464         }
465
466         // Set the native window's buffers format to match what this config requests.
467         // Whether to use sRGB gamma is not part of the EGLconfig, but is part
468         // of our native format. So if sRGB gamma is requested, we have to
469         // modify the EGLconfig's format before setting the native window's
470         // format.
471
472         // by default, just pick RGBA_8888
473         EGLint format = HAL_PIXEL_FORMAT_RGBA_8888;
474         android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN;
475
476         EGLint a = 0;
477         cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_ALPHA_SIZE, &a);
478         if (a > 0) {
479             // alpha-channel requested, there's really only one suitable format
480             format = HAL_PIXEL_FORMAT_RGBA_8888;
481         } else {
482             EGLint r, g, b;
483             r = g = b = 0;
484             cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_RED_SIZE,   &r);
485             cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_GREEN_SIZE, &g);
486             cnx->egl.eglGetConfigAttrib(iDpy, config, EGL_BLUE_SIZE,  &b);
487             EGLint colorDepth = r + g + b;
488             if (colorDepth <= 16) {
489                 format = HAL_PIXEL_FORMAT_RGB_565;
490             } else {
491                 format = HAL_PIXEL_FORMAT_RGBX_8888;
492             }
493         }
494
495         // now select a corresponding sRGB format if needed
496         if (attrib_list && dp->haveExtension("EGL_KHR_gl_colorspace")) {
497             for (const EGLint* attr = attrib_list; *attr != EGL_NONE; attr += 2) {
498                 if (*attr == EGL_GL_COLORSPACE_KHR) {
499                     if (ENABLE_EGL_KHR_GL_COLORSPACE) {
500                         dataSpace = modifyBufferDataspace(dataSpace, *(attr+1));
501                     } else {
502                         // Normally we'd pass through unhandled attributes to
503                         // the driver. But in case the driver implements this
504                         // extension but we're disabling it, we want to prevent
505                         // it getting through -- support will be broken without
506                         // our help.
507                         ALOGE("sRGB window surfaces not supported");
508                         return setError(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
509                     }
510                 }
511             }
512         }
513
514         if (format != 0) {
515             int err = native_window_set_buffers_format(window, format);
516             if (err != 0) {
517                 ALOGE("error setting native window pixel format: %s (%d)",
518                         strerror(-err), err);
519                 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
520                 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
521             }
522         }
523
524         if (dataSpace != 0) {
525             int err = native_window_set_buffers_data_space(window, dataSpace);
526             if (err != 0) {
527                 ALOGE("error setting native window pixel dataSpace: %s (%d)",
528                         strerror(-err), err);
529                 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
530                 return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
531             }
532         }
533
534         // the EGL spec requires that a new EGLSurface default to swap interval
535         // 1, so explicitly set that on the window here.
536         ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window);
537         anw->setSwapInterval(anw, 1);
538
539         EGLSurface surface = cnx->egl.eglCreateWindowSurface(
540                 iDpy, config, window, attrib_list);
541         if (surface != EGL_NO_SURFACE) {
542             egl_surface_t* s = new egl_surface_t(dp.get(), config, window,
543                     surface, cnx);
544             return s;
545         }
546
547         // EGLSurface creation failed
548         native_window_set_buffers_format(window, 0);
549         native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
550     }
551     return EGL_NO_SURFACE;
552 }
553
554 EGLSurface eglCreatePixmapSurface(  EGLDisplay dpy, EGLConfig config,
555                                     NativePixmapType pixmap,
556                                     const EGLint *attrib_list)
557 {
558     clearError();
559
560     egl_connection_t* cnx = NULL;
561     egl_display_ptr dp = validate_display_connection(dpy, cnx);
562     if (dp) {
563         EGLSurface surface = cnx->egl.eglCreatePixmapSurface(
564                 dp->disp.dpy, config, pixmap, attrib_list);
565         if (surface != EGL_NO_SURFACE) {
566             egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
567                     surface, cnx);
568             return s;
569         }
570     }
571     return EGL_NO_SURFACE;
572 }
573
574 EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config,
575                                     const EGLint *attrib_list)
576 {
577     clearError();
578
579     egl_connection_t* cnx = NULL;
580     egl_display_ptr dp = validate_display_connection(dpy, cnx);
581     if (dp) {
582         EGLSurface surface = cnx->egl.eglCreatePbufferSurface(
583                 dp->disp.dpy, config, attrib_list);
584         if (surface != EGL_NO_SURFACE) {
585             egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
586                     surface, cnx);
587             return s;
588         }
589     }
590     return EGL_NO_SURFACE;
591 }
592
593 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
594 {
595     clearError();
596
597     const egl_display_ptr dp = validate_display(dpy);
598     if (!dp) return EGL_FALSE;
599
600     SurfaceRef _s(dp.get(), surface);
601     if (!_s.get())
602         return setError(EGL_BAD_SURFACE, EGL_FALSE);
603
604     egl_surface_t * const s = get_surface(surface);
605     EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface);
606     if (result == EGL_TRUE) {
607         _s.terminate();
608     }
609     return result;
610 }
611
612 EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface,
613                             EGLint attribute, EGLint *value)
614 {
615     clearError();
616
617     const egl_display_ptr dp = validate_display(dpy);
618     if (!dp) return EGL_FALSE;
619
620     SurfaceRef _s(dp.get(), surface);
621     if (!_s.get())
622         return setError(EGL_BAD_SURFACE, EGL_FALSE);
623
624     egl_surface_t const * const s = get_surface(surface);
625     return s->cnx->egl.eglQuerySurface(
626             dp->disp.dpy, s->surface, attribute, value);
627 }
628
629 void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) {
630     ATRACE_CALL();
631     clearError();
632
633     const egl_display_ptr dp = validate_display(dpy);
634     if (!dp) {
635         return;
636     }
637
638     SurfaceRef _s(dp.get(), surface);
639     if (!_s.get()) {
640         setError(EGL_BAD_SURFACE, EGL_FALSE);
641         return;
642     }
643 }
644
645 // ----------------------------------------------------------------------------
646 // Contexts
647 // ----------------------------------------------------------------------------
648
649 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config,
650                             EGLContext share_list, const EGLint *attrib_list)
651 {
652     clearError();
653
654     egl_connection_t* cnx = NULL;
655     const egl_display_ptr dp = validate_display_connection(dpy, cnx);
656     if (dp) {
657         if (share_list != EGL_NO_CONTEXT) {
658             if (!ContextRef(dp.get(), share_list).get()) {
659                 return setError(EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
660             }
661             egl_context_t* const c = get_context(share_list);
662             share_list = c->context;
663         }
664         EGLContext context = cnx->egl.eglCreateContext(
665                 dp->disp.dpy, config, share_list, attrib_list);
666         if (context != EGL_NO_CONTEXT) {
667             // figure out if it's a GLESv1 or GLESv2
668             int version = 0;
669             if (attrib_list) {
670                 while (*attrib_list != EGL_NONE) {
671                     GLint attr = *attrib_list++;
672                     GLint value = *attrib_list++;
673                     if (attr == EGL_CONTEXT_CLIENT_VERSION) {
674                         if (value == 1) {
675                             version = egl_connection_t::GLESv1_INDEX;
676                         } else if (value == 2 || value == 3) {
677                             version = egl_connection_t::GLESv2_INDEX;
678                         }
679                     }
680                 };
681             }
682             egl_context_t* c = new egl_context_t(dpy, context, config, cnx,
683                     version);
684             return c;
685         }
686     }
687     return EGL_NO_CONTEXT;
688 }
689
690 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
691 {
692     clearError();
693
694     const egl_display_ptr dp = validate_display(dpy);
695     if (!dp)
696         return EGL_FALSE;
697
698     ContextRef _c(dp.get(), ctx);
699     if (!_c.get())
700         return setError(EGL_BAD_CONTEXT, EGL_FALSE);
701
702     egl_context_t * const c = get_context(ctx);
703     EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context);
704     if (result == EGL_TRUE) {
705         _c.terminate();
706     }
707     return result;
708 }
709
710 EGLBoolean eglMakeCurrent(  EGLDisplay dpy, EGLSurface draw,
711                             EGLSurface read, EGLContext ctx)
712 {
713     clearError();
714
715     egl_display_ptr dp = validate_display(dpy);
716     if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE);
717
718     // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not
719     // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is
720     // a valid but uninitialized display.
721     if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) ||
722          (draw != EGL_NO_SURFACE) ) {
723         if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
724     }
725
726     // get a reference to the object passed in
727     ContextRef _c(dp.get(), ctx);
728     SurfaceRef _d(dp.get(), draw);
729     SurfaceRef _r(dp.get(), read);
730
731     // validate the context (if not EGL_NO_CONTEXT)
732     if ((ctx != EGL_NO_CONTEXT) && !_c.get()) {
733         // EGL_NO_CONTEXT is valid
734         return setError(EGL_BAD_CONTEXT, EGL_FALSE);
735     }
736
737     // these are the underlying implementation's object
738     EGLContext impl_ctx  = EGL_NO_CONTEXT;
739     EGLSurface impl_draw = EGL_NO_SURFACE;
740     EGLSurface impl_read = EGL_NO_SURFACE;
741
742     // these are our objects structs passed in
743     egl_context_t       * c = NULL;
744     egl_surface_t const * d = NULL;
745     egl_surface_t const * r = NULL;
746
747     // these are the current objects structs
748     egl_context_t * cur_c = get_context(getContext());
749
750     if (ctx != EGL_NO_CONTEXT) {
751         c = get_context(ctx);
752         impl_ctx = c->context;
753     } else {
754         // no context given, use the implementation of the current context
755         if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) {
756             // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT);
757             return setError(EGL_BAD_MATCH, EGL_FALSE);
758         }
759         if (cur_c == NULL) {
760             // no current context
761             // not an error, there is just no current context.
762             return EGL_TRUE;
763         }
764     }
765
766     // retrieve the underlying implementation's draw EGLSurface
767     if (draw != EGL_NO_SURFACE) {
768         if (!_d.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
769         d = get_surface(draw);
770         impl_draw = d->surface;
771     }
772
773     // retrieve the underlying implementation's read EGLSurface
774     if (read != EGL_NO_SURFACE) {
775         if (!_r.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE);
776         r = get_surface(read);
777         impl_read = r->surface;
778     }
779
780
781     EGLBoolean result = dp->makeCurrent(c, cur_c,
782             draw, read, ctx,
783             impl_draw, impl_read, impl_ctx);
784
785     if (result == EGL_TRUE) {
786         if (c) {
787             setGLHooksThreadSpecific(c->cnx->hooks[c->version]);
788             egl_tls_t::setContext(ctx);
789             _c.acquire();
790             _r.acquire();
791             _d.acquire();
792         } else {
793             setGLHooksThreadSpecific(&gHooksNoContext);
794             egl_tls_t::setContext(EGL_NO_CONTEXT);
795         }
796     } else {
797         // this will ALOGE the error
798         egl_connection_t* const cnx = &gEGLImpl;
799         result = setError(cnx->egl.eglGetError(), EGL_FALSE);
800     }
801     return result;
802 }
803
804
805 EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx,
806                             EGLint attribute, EGLint *value)
807 {
808     clearError();
809
810     const egl_display_ptr dp = validate_display(dpy);
811     if (!dp) return EGL_FALSE;
812
813     ContextRef _c(dp.get(), ctx);
814     if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE);
815
816     egl_context_t * const c = get_context(ctx);
817     return c->cnx->egl.eglQueryContext(
818             dp->disp.dpy, c->context, attribute, value);
819
820 }
821
822 EGLContext eglGetCurrentContext(void)
823 {
824     // could be called before eglInitialize(), but we wouldn't have a context
825     // then, and this function would correctly return EGL_NO_CONTEXT.
826
827     clearError();
828
829     EGLContext ctx = getContext();
830     return ctx;
831 }
832
833 EGLSurface eglGetCurrentSurface(EGLint readdraw)
834 {
835     // could be called before eglInitialize(), but we wouldn't have a context
836     // then, and this function would correctly return EGL_NO_SURFACE.
837
838     clearError();
839
840     EGLContext ctx = getContext();
841     if (ctx) {
842         egl_context_t const * const c = get_context(ctx);
843         if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
844         switch (readdraw) {
845             case EGL_READ: return c->read;
846             case EGL_DRAW: return c->draw;
847             default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
848         }
849     }
850     return EGL_NO_SURFACE;
851 }
852
853 EGLDisplay eglGetCurrentDisplay(void)
854 {
855     // could be called before eglInitialize(), but we wouldn't have a context
856     // then, and this function would correctly return EGL_NO_DISPLAY.
857
858     clearError();
859
860     EGLContext ctx = getContext();
861     if (ctx) {
862         egl_context_t const * const c = get_context(ctx);
863         if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE);
864         return c->dpy;
865     }
866     return EGL_NO_DISPLAY;
867 }
868
869 EGLBoolean eglWaitGL(void)
870 {
871     clearError();
872
873     egl_connection_t* const cnx = &gEGLImpl;
874     if (!cnx->dso)
875         return setError(EGL_BAD_CONTEXT, EGL_FALSE);
876
877     return cnx->egl.eglWaitGL();
878 }
879
880 EGLBoolean eglWaitNative(EGLint engine)
881 {
882     clearError();
883
884     egl_connection_t* const cnx = &gEGLImpl;
885     if (!cnx->dso)
886         return setError(EGL_BAD_CONTEXT, EGL_FALSE);
887
888     return cnx->egl.eglWaitNative(engine);
889 }
890
891 EGLint eglGetError(void)
892 {
893     EGLint err = EGL_SUCCESS;
894     egl_connection_t* const cnx = &gEGLImpl;
895     if (cnx->dso) {
896         err = cnx->egl.eglGetError();
897     }
898     if (err == EGL_SUCCESS) {
899         err = egl_tls_t::getError();
900     }
901     return err;
902 }
903
904 static __eglMustCastToProperFunctionPointerType findBuiltinWrapper(
905         const char* procname) {
906     const egl_connection_t* cnx = &gEGLImpl;
907     void* proc = NULL;
908
909     proc = dlsym(cnx->libEgl, procname);
910     if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
911
912     proc = dlsym(cnx->libGles2, procname);
913     if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
914
915     proc = dlsym(cnx->libGles1, procname);
916     if (proc) return (__eglMustCastToProperFunctionPointerType)proc;
917
918     return NULL;
919 }
920
921 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
922 {
923     // eglGetProcAddress() could be the very first function called
924     // in which case we must make sure we've initialized ourselves, this
925     // happens the first time egl_get_display() is called.
926
927     clearError();
928
929     if (egl_init_drivers() == EGL_FALSE) {
930         setError(EGL_BAD_PARAMETER, NULL);
931         return  NULL;
932     }
933
934     if (FILTER_EXTENSIONS(procname)) {
935         return NULL;
936     }
937
938     __eglMustCastToProperFunctionPointerType addr;
939     addr = findProcAddress(procname, sExtensionMap, NELEM(sExtensionMap));
940     if (addr) return addr;
941
942     addr = findBuiltinWrapper(procname);
943     if (addr) return addr;
944
945     // this protects accesses to sGLExtentionMap and sGLExtentionSlot
946     pthread_mutex_lock(&sExtensionMapMutex);
947
948         /*
949          * Since eglGetProcAddress() is not associated to anything, it needs
950          * to return a function pointer that "works" regardless of what
951          * the current context is.
952          *
953          * For this reason, we return a "forwarder", a small stub that takes
954          * care of calling the function associated with the context
955          * currently bound.
956          *
957          * We first look for extensions we've already resolved, if we're seeing
958          * this extension for the first time, we go through all our
959          * implementations and call eglGetProcAddress() and record the
960          * result in the appropriate implementation hooks and return the
961          * address of the forwarder corresponding to that hook set.
962          *
963          */
964
965         const String8 name(procname);
966         addr = sGLExtentionMap.valueFor(name);
967         const int slot = sGLExtentionSlot;
968
969         ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS,
970                 "no more slots for eglGetProcAddress(\"%s\")",
971                 procname);
972
973         if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) {
974             bool found = false;
975
976             egl_connection_t* const cnx = &gEGLImpl;
977             if (cnx->dso && cnx->egl.eglGetProcAddress) {
978                 // Extensions are independent of the bound context
979                 addr =
980                 cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] =
981                 cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] =
982                         cnx->egl.eglGetProcAddress(procname);
983                 if (addr) found = true;
984             }
985
986             if (found) {
987                 addr = gExtensionForwarders[slot];
988                 sGLExtentionMap.add(name, addr);
989                 sGLExtentionSlot++;
990             }
991         }
992
993     pthread_mutex_unlock(&sExtensionMapMutex);
994     return addr;
995 }
996
997 class FrameCompletionThread : public Thread {
998 public:
999
1000     static void queueSync(EGLSyncKHR sync) {
1001         static sp<FrameCompletionThread> thread(new FrameCompletionThread);
1002         static bool running = false;
1003         if (!running) {
1004             thread->run("GPUFrameCompletion");
1005             running = true;
1006         }
1007         {
1008             Mutex::Autolock lock(thread->mMutex);
1009             ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d",
1010                     thread->mFramesQueued).string());
1011             thread->mQueue.push_back(sync);
1012             thread->mCondition.signal();
1013             thread->mFramesQueued++;
1014             ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size());
1015         }
1016     }
1017
1018 private:
1019     FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {}
1020
1021     virtual bool threadLoop() {
1022         EGLSyncKHR sync;
1023         uint32_t frameNum;
1024         {
1025             Mutex::Autolock lock(mMutex);
1026             while (mQueue.isEmpty()) {
1027                 mCondition.wait(mMutex);
1028             }
1029             sync = mQueue[0];
1030             frameNum = mFramesCompleted;
1031         }
1032         EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1033         {
1034             ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d",
1035                     frameNum).string());
1036             EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR);
1037             if (result == EGL_FALSE) {
1038                 ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError());
1039             } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
1040                 ALOGE("FrameCompletion: timeout waiting for fence");
1041             }
1042             eglDestroySyncKHR(dpy, sync);
1043         }
1044         {
1045             Mutex::Autolock lock(mMutex);
1046             mQueue.removeAt(0);
1047             mFramesCompleted++;
1048             ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1049         }
1050         return true;
1051     }
1052
1053     uint32_t mFramesQueued;
1054     uint32_t mFramesCompleted;
1055     Vector<EGLSyncKHR> mQueue;
1056     Condition mCondition;
1057     Mutex mMutex;
1058 };
1059
1060 EGLBoolean eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface draw,
1061         EGLint *rects, EGLint n_rects)
1062 {
1063     ATRACE_CALL();
1064     clearError();
1065
1066     const egl_display_ptr dp = validate_display(dpy);
1067     if (!dp) return EGL_FALSE;
1068
1069     SurfaceRef _s(dp.get(), draw);
1070     if (!_s.get())
1071         return setError(EGL_BAD_SURFACE, EGL_FALSE);
1072
1073     egl_surface_t const * const s = get_surface(draw);
1074
1075     if (CC_UNLIKELY(dp->traceGpuCompletion)) {
1076         EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL);
1077         if (sync != EGL_NO_SYNC_KHR) {
1078             FrameCompletionThread::queueSync(sync);
1079         }
1080     }
1081
1082     if (CC_UNLIKELY(dp->finishOnSwap)) {
1083         uint32_t pixel;
1084         egl_context_t * const c = get_context( egl_tls_t::getContext() );
1085         if (c) {
1086             // glReadPixels() ensures that the frame is complete
1087             s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1,
1088                     GL_RGBA,GL_UNSIGNED_BYTE,&pixel);
1089         }
1090     }
1091
1092     if (n_rects == 0) {
1093         return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1094     }
1095
1096     Vector<android_native_rect_t> androidRects;
1097     for (int r = 0; r < n_rects; ++r) {
1098         int offset = r * 4;
1099         int x = rects[offset];
1100         int y = rects[offset + 1];
1101         int width = rects[offset + 2];
1102         int height = rects[offset + 3];
1103         android_native_rect_t androidRect;
1104         androidRect.left = x;
1105         androidRect.top = y + height;
1106         androidRect.right = x + width;
1107         androidRect.bottom = y;
1108         androidRects.push_back(androidRect);
1109     }
1110     native_window_set_surface_damage(s->win.get(), androidRects.array(),
1111             androidRects.size());
1112
1113     if (s->cnx->egl.eglSwapBuffersWithDamageKHR) {
1114         return s->cnx->egl.eglSwapBuffersWithDamageKHR(dp->disp.dpy, s->surface,
1115                 rects, n_rects);
1116     } else {
1117         return s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface);
1118     }
1119 }
1120
1121 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
1122 {
1123     return eglSwapBuffersWithDamageKHR(dpy, surface, NULL, 0);
1124 }
1125
1126 EGLBoolean eglCopyBuffers(  EGLDisplay dpy, EGLSurface surface,
1127                             NativePixmapType target)
1128 {
1129     clearError();
1130
1131     const egl_display_ptr dp = validate_display(dpy);
1132     if (!dp) return EGL_FALSE;
1133
1134     SurfaceRef _s(dp.get(), surface);
1135     if (!_s.get())
1136         return setError(EGL_BAD_SURFACE, EGL_FALSE);
1137
1138     egl_surface_t const * const s = get_surface(surface);
1139     return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target);
1140 }
1141
1142 const char* eglQueryString(EGLDisplay dpy, EGLint name)
1143 {
1144     clearError();
1145
1146     const egl_display_ptr dp = validate_display(dpy);
1147     if (!dp) return (const char *) NULL;
1148
1149     switch (name) {
1150         case EGL_VENDOR:
1151             return dp->getVendorString();
1152         case EGL_VERSION:
1153             return dp->getVersionString();
1154         case EGL_EXTENSIONS:
1155             return dp->getExtensionString();
1156         case EGL_CLIENT_APIS:
1157             return dp->getClientApiString();
1158     }
1159     return setError(EGL_BAD_PARAMETER, (const char *)0);
1160 }
1161
1162 EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name)
1163 {
1164     clearError();
1165
1166     const egl_display_ptr dp = validate_display(dpy);
1167     if (!dp) return (const char *) NULL;
1168
1169     switch (name) {
1170         case EGL_VENDOR:
1171             return dp->disp.queryString.vendor;
1172         case EGL_VERSION:
1173             return dp->disp.queryString.version;
1174         case EGL_EXTENSIONS:
1175             return dp->disp.queryString.extensions;
1176         case EGL_CLIENT_APIS:
1177             return dp->disp.queryString.clientApi;
1178     }
1179     return setError(EGL_BAD_PARAMETER, (const char *)0);
1180 }
1181
1182 // ----------------------------------------------------------------------------
1183 // EGL 1.1
1184 // ----------------------------------------------------------------------------
1185
1186 EGLBoolean eglSurfaceAttrib(
1187         EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1188 {
1189     clearError();
1190
1191     const egl_display_ptr dp = validate_display(dpy);
1192     if (!dp) return EGL_FALSE;
1193
1194     SurfaceRef _s(dp.get(), surface);
1195     if (!_s.get())
1196         return setError(EGL_BAD_SURFACE, EGL_FALSE);
1197
1198     egl_surface_t const * const s = get_surface(surface);
1199
1200     if (attribute == EGL_FRONT_BUFFER_AUTO_REFRESH_ANDROID) {
1201         int err = native_window_set_auto_refresh(s->win.get(),
1202             value ? true : false);
1203         return (err == NO_ERROR) ? EGL_TRUE :
1204             setError(EGL_BAD_SURFACE, EGL_FALSE);
1205     }
1206
1207     if (s->cnx->egl.eglSurfaceAttrib) {
1208         return s->cnx->egl.eglSurfaceAttrib(
1209                 dp->disp.dpy, s->surface, attribute, value);
1210     }
1211     return setError(EGL_BAD_SURFACE, EGL_FALSE);
1212 }
1213
1214 EGLBoolean eglBindTexImage(
1215         EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1216 {
1217     clearError();
1218
1219     const egl_display_ptr dp = validate_display(dpy);
1220     if (!dp) return EGL_FALSE;
1221
1222     SurfaceRef _s(dp.get(), surface);
1223     if (!_s.get())
1224         return setError(EGL_BAD_SURFACE, EGL_FALSE);
1225
1226     egl_surface_t const * const s = get_surface(surface);
1227     if (s->cnx->egl.eglBindTexImage) {
1228         return s->cnx->egl.eglBindTexImage(
1229                 dp->disp.dpy, s->surface, buffer);
1230     }
1231     return setError(EGL_BAD_SURFACE, EGL_FALSE);
1232 }
1233
1234 EGLBoolean eglReleaseTexImage(
1235         EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1236 {
1237     clearError();
1238
1239     const egl_display_ptr dp = validate_display(dpy);
1240     if (!dp) return EGL_FALSE;
1241
1242     SurfaceRef _s(dp.get(), surface);
1243     if (!_s.get())
1244         return setError(EGL_BAD_SURFACE, EGL_FALSE);
1245
1246     egl_surface_t const * const s = get_surface(surface);
1247     if (s->cnx->egl.eglReleaseTexImage) {
1248         return s->cnx->egl.eglReleaseTexImage(
1249                 dp->disp.dpy, s->surface, buffer);
1250     }
1251     return setError(EGL_BAD_SURFACE, EGL_FALSE);
1252 }
1253
1254 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1255 {
1256     clearError();
1257
1258     const egl_display_ptr dp = validate_display(dpy);
1259     if (!dp) return EGL_FALSE;
1260
1261     EGLBoolean res = EGL_TRUE;
1262     egl_connection_t* const cnx = &gEGLImpl;
1263     if (cnx->dso && cnx->egl.eglSwapInterval) {
1264         res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval);
1265     }
1266
1267     return res;
1268 }
1269
1270
1271 // ----------------------------------------------------------------------------
1272 // EGL 1.2
1273 // ----------------------------------------------------------------------------
1274
1275 EGLBoolean eglWaitClient(void)
1276 {
1277     clearError();
1278
1279     egl_connection_t* const cnx = &gEGLImpl;
1280     if (!cnx->dso)
1281         return setError(EGL_BAD_CONTEXT, EGL_FALSE);
1282
1283     EGLBoolean res;
1284     if (cnx->egl.eglWaitClient) {
1285         res = cnx->egl.eglWaitClient();
1286     } else {
1287         res = cnx->egl.eglWaitGL();
1288     }
1289     return res;
1290 }
1291
1292 EGLBoolean eglBindAPI(EGLenum api)
1293 {
1294     clearError();
1295
1296     if (egl_init_drivers() == EGL_FALSE) {
1297         return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1298     }
1299
1300     // bind this API on all EGLs
1301     EGLBoolean res = EGL_TRUE;
1302     egl_connection_t* const cnx = &gEGLImpl;
1303     if (cnx->dso && cnx->egl.eglBindAPI) {
1304         res = cnx->egl.eglBindAPI(api);
1305     }
1306     return res;
1307 }
1308
1309 EGLenum eglQueryAPI(void)
1310 {
1311     clearError();
1312
1313     if (egl_init_drivers() == EGL_FALSE) {
1314         return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1315     }
1316
1317     egl_connection_t* const cnx = &gEGLImpl;
1318     if (cnx->dso && cnx->egl.eglQueryAPI) {
1319         return cnx->egl.eglQueryAPI();
1320     }
1321
1322     // or, it can only be OpenGL ES
1323     return EGL_OPENGL_ES_API;
1324 }
1325
1326 EGLBoolean eglReleaseThread(void)
1327 {
1328     clearError();
1329
1330     // If there is context bound to the thread, release it
1331     egl_display_t::loseCurrent(get_context(getContext()));
1332
1333     egl_connection_t* const cnx = &gEGLImpl;
1334     if (cnx->dso && cnx->egl.eglReleaseThread) {
1335         cnx->egl.eglReleaseThread();
1336     }
1337     egl_tls_t::clearTLS();
1338     return EGL_TRUE;
1339 }
1340
1341 EGLSurface eglCreatePbufferFromClientBuffer(
1342           EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer,
1343           EGLConfig config, const EGLint *attrib_list)
1344 {
1345     clearError();
1346
1347     egl_connection_t* cnx = NULL;
1348     const egl_display_ptr dp = validate_display_connection(dpy, cnx);
1349     if (!dp) return EGL_FALSE;
1350     if (cnx->egl.eglCreatePbufferFromClientBuffer) {
1351         return cnx->egl.eglCreatePbufferFromClientBuffer(
1352                 dp->disp.dpy, buftype, buffer, config, attrib_list);
1353     }
1354     return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE);
1355 }
1356
1357 // ----------------------------------------------------------------------------
1358 // EGL_EGLEXT_VERSION 3
1359 // ----------------------------------------------------------------------------
1360
1361 EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface,
1362         const EGLint *attrib_list)
1363 {
1364     clearError();
1365
1366     const egl_display_ptr dp = validate_display(dpy);
1367     if (!dp) return EGL_FALSE;
1368
1369     SurfaceRef _s(dp.get(), surface);
1370     if (!_s.get())
1371         return setError(EGL_BAD_SURFACE, EGL_FALSE);
1372
1373     egl_surface_t const * const s = get_surface(surface);
1374     if (s->cnx->egl.eglLockSurfaceKHR) {
1375         return s->cnx->egl.eglLockSurfaceKHR(
1376                 dp->disp.dpy, s->surface, attrib_list);
1377     }
1378     return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1379 }
1380
1381 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface)
1382 {
1383     clearError();
1384
1385     const egl_display_ptr dp = validate_display(dpy);
1386     if (!dp) return EGL_FALSE;
1387
1388     SurfaceRef _s(dp.get(), surface);
1389     if (!_s.get())
1390         return setError(EGL_BAD_SURFACE, EGL_FALSE);
1391
1392     egl_surface_t const * const s = get_surface(surface);
1393     if (s->cnx->egl.eglUnlockSurfaceKHR) {
1394         return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface);
1395     }
1396     return setError(EGL_BAD_DISPLAY, EGL_FALSE);
1397 }
1398
1399 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
1400         EGLClientBuffer buffer, const EGLint *attrib_list)
1401 {
1402     clearError();
1403
1404     const egl_display_ptr dp = validate_display(dpy);
1405     if (!dp) return EGL_NO_IMAGE_KHR;
1406
1407     ContextRef _c(dp.get(), ctx);
1408     egl_context_t * const c = _c.get();
1409
1410     EGLImageKHR result = EGL_NO_IMAGE_KHR;
1411     egl_connection_t* const cnx = &gEGLImpl;
1412     if (cnx->dso && cnx->egl.eglCreateImageKHR) {
1413         result = cnx->egl.eglCreateImageKHR(
1414                 dp->disp.dpy,
1415                 c ? c->context : EGL_NO_CONTEXT,
1416                 target, buffer, attrib_list);
1417     }
1418     return result;
1419 }
1420
1421 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1422 {
1423     clearError();
1424
1425     const egl_display_ptr dp = validate_display(dpy);
1426     if (!dp) return EGL_FALSE;
1427
1428     EGLBoolean result = EGL_FALSE;
1429     egl_connection_t* const cnx = &gEGLImpl;
1430     if (cnx->dso && cnx->egl.eglDestroyImageKHR) {
1431         result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img);
1432     }
1433     return result;
1434 }
1435
1436 // ----------------------------------------------------------------------------
1437 // EGL_EGLEXT_VERSION 5
1438 // ----------------------------------------------------------------------------
1439
1440
1441 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list)
1442 {
1443     clearError();
1444
1445     const egl_display_ptr dp = validate_display(dpy);
1446     if (!dp) return EGL_NO_SYNC_KHR;
1447
1448     EGLSyncKHR result = EGL_NO_SYNC_KHR;
1449     egl_connection_t* const cnx = &gEGLImpl;
1450     if (cnx->dso && cnx->egl.eglCreateSyncKHR) {
1451         result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list);
1452     }
1453     return result;
1454 }
1455
1456 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync)
1457 {
1458     clearError();
1459
1460     const egl_display_ptr dp = validate_display(dpy);
1461     if (!dp) return EGL_FALSE;
1462
1463     EGLBoolean result = EGL_FALSE;
1464     egl_connection_t* const cnx = &gEGLImpl;
1465     if (cnx->dso && cnx->egl.eglDestroySyncKHR) {
1466         result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync);
1467     }
1468     return result;
1469 }
1470
1471 EGLBoolean eglSignalSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLenum mode) {
1472     clearError();
1473
1474     const egl_display_ptr dp = validate_display(dpy);
1475     if (!dp) return EGL_FALSE;
1476
1477     EGLBoolean result = EGL_FALSE;
1478     egl_connection_t* const cnx = &gEGLImpl;
1479     if (cnx->dso && cnx->egl.eglSignalSyncKHR) {
1480         result = cnx->egl.eglSignalSyncKHR(
1481                 dp->disp.dpy, sync, mode);
1482     }
1483     return result;
1484 }
1485
1486 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync,
1487         EGLint flags, EGLTimeKHR timeout)
1488 {
1489     clearError();
1490
1491     const egl_display_ptr dp = validate_display(dpy);
1492     if (!dp) return EGL_FALSE;
1493
1494     EGLBoolean result = EGL_FALSE;
1495     egl_connection_t* const cnx = &gEGLImpl;
1496     if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) {
1497         result = cnx->egl.eglClientWaitSyncKHR(
1498                 dp->disp.dpy, sync, flags, timeout);
1499     }
1500     return result;
1501 }
1502
1503 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync,
1504         EGLint attribute, EGLint *value)
1505 {
1506     clearError();
1507
1508     const egl_display_ptr dp = validate_display(dpy);
1509     if (!dp) return EGL_FALSE;
1510
1511     EGLBoolean result = EGL_FALSE;
1512     egl_connection_t* const cnx = &gEGLImpl;
1513     if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) {
1514         result = cnx->egl.eglGetSyncAttribKHR(
1515                 dp->disp.dpy, sync, attribute, value);
1516     }
1517     return result;
1518 }
1519
1520 EGLStreamKHR eglCreateStreamKHR(EGLDisplay dpy, const EGLint *attrib_list)
1521 {
1522     clearError();
1523
1524     const egl_display_ptr dp = validate_display(dpy);
1525     if (!dp) return EGL_NO_STREAM_KHR;
1526
1527     EGLStreamKHR result = EGL_NO_STREAM_KHR;
1528     egl_connection_t* const cnx = &gEGLImpl;
1529     if (cnx->dso && cnx->egl.eglCreateStreamKHR) {
1530         result = cnx->egl.eglCreateStreamKHR(
1531                 dp->disp.dpy, attrib_list);
1532     }
1533     return result;
1534 }
1535
1536 EGLBoolean eglDestroyStreamKHR(EGLDisplay dpy, EGLStreamKHR stream)
1537 {
1538     clearError();
1539
1540     const egl_display_ptr dp = validate_display(dpy);
1541     if (!dp) return EGL_FALSE;
1542
1543     EGLBoolean result = EGL_FALSE;
1544     egl_connection_t* const cnx = &gEGLImpl;
1545     if (cnx->dso && cnx->egl.eglDestroyStreamKHR) {
1546         result = cnx->egl.eglDestroyStreamKHR(
1547                 dp->disp.dpy, stream);
1548     }
1549     return result;
1550 }
1551
1552 EGLBoolean eglStreamAttribKHR(EGLDisplay dpy, EGLStreamKHR stream,
1553         EGLenum attribute, EGLint value)
1554 {
1555     clearError();
1556
1557     const egl_display_ptr dp = validate_display(dpy);
1558     if (!dp) return EGL_FALSE;
1559
1560     EGLBoolean result = EGL_FALSE;
1561     egl_connection_t* const cnx = &gEGLImpl;
1562     if (cnx->dso && cnx->egl.eglStreamAttribKHR) {
1563         result = cnx->egl.eglStreamAttribKHR(
1564                 dp->disp.dpy, stream, attribute, value);
1565     }
1566     return result;
1567 }
1568
1569 EGLBoolean eglQueryStreamKHR(EGLDisplay dpy, EGLStreamKHR stream,
1570         EGLenum attribute, EGLint *value)
1571 {
1572     clearError();
1573
1574     const egl_display_ptr dp = validate_display(dpy);
1575     if (!dp) return EGL_FALSE;
1576
1577     EGLBoolean result = EGL_FALSE;
1578     egl_connection_t* const cnx = &gEGLImpl;
1579     if (cnx->dso && cnx->egl.eglQueryStreamKHR) {
1580         result = cnx->egl.eglQueryStreamKHR(
1581                 dp->disp.dpy, stream, attribute, value);
1582     }
1583     return result;
1584 }
1585
1586 EGLBoolean eglQueryStreamu64KHR(EGLDisplay dpy, EGLStreamKHR stream,
1587         EGLenum attribute, EGLuint64KHR *value)
1588 {
1589     clearError();
1590
1591     const egl_display_ptr dp = validate_display(dpy);
1592     if (!dp) return EGL_FALSE;
1593
1594     EGLBoolean result = EGL_FALSE;
1595     egl_connection_t* const cnx = &gEGLImpl;
1596     if (cnx->dso && cnx->egl.eglQueryStreamu64KHR) {
1597         result = cnx->egl.eglQueryStreamu64KHR(
1598                 dp->disp.dpy, stream, attribute, value);
1599     }
1600     return result;
1601 }
1602
1603 EGLBoolean eglQueryStreamTimeKHR(EGLDisplay dpy, EGLStreamKHR stream,
1604         EGLenum attribute, EGLTimeKHR *value)
1605 {
1606     clearError();
1607
1608     const egl_display_ptr dp = validate_display(dpy);
1609     if (!dp) return EGL_FALSE;
1610
1611     EGLBoolean result = EGL_FALSE;
1612     egl_connection_t* const cnx = &gEGLImpl;
1613     if (cnx->dso && cnx->egl.eglQueryStreamTimeKHR) {
1614         result = cnx->egl.eglQueryStreamTimeKHR(
1615                 dp->disp.dpy, stream, attribute, value);
1616     }
1617     return result;
1618 }
1619
1620 EGLSurface eglCreateStreamProducerSurfaceKHR(EGLDisplay dpy, EGLConfig config,
1621         EGLStreamKHR stream, const EGLint *attrib_list)
1622 {
1623     clearError();
1624
1625     egl_display_ptr dp = validate_display(dpy);
1626     if (!dp) return EGL_NO_SURFACE;
1627
1628     egl_connection_t* const cnx = &gEGLImpl;
1629     if (cnx->dso && cnx->egl.eglCreateStreamProducerSurfaceKHR) {
1630         EGLSurface surface = cnx->egl.eglCreateStreamProducerSurfaceKHR(
1631                 dp->disp.dpy, config, stream, attrib_list);
1632         if (surface != EGL_NO_SURFACE) {
1633             egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL,
1634                     surface, cnx);
1635             return s;
1636         }
1637     }
1638     return EGL_NO_SURFACE;
1639 }
1640
1641 EGLBoolean eglStreamConsumerGLTextureExternalKHR(EGLDisplay dpy,
1642         EGLStreamKHR stream)
1643 {
1644     clearError();
1645
1646     const egl_display_ptr dp = validate_display(dpy);
1647     if (!dp) return EGL_FALSE;
1648
1649     EGLBoolean result = EGL_FALSE;
1650     egl_connection_t* const cnx = &gEGLImpl;
1651     if (cnx->dso && cnx->egl.eglStreamConsumerGLTextureExternalKHR) {
1652         result = cnx->egl.eglStreamConsumerGLTextureExternalKHR(
1653                 dp->disp.dpy, stream);
1654     }
1655     return result;
1656 }
1657
1658 EGLBoolean eglStreamConsumerAcquireKHR(EGLDisplay dpy,
1659         EGLStreamKHR stream)
1660 {
1661     clearError();
1662
1663     const egl_display_ptr dp = validate_display(dpy);
1664     if (!dp) return EGL_FALSE;
1665
1666     EGLBoolean result = EGL_FALSE;
1667     egl_connection_t* const cnx = &gEGLImpl;
1668     if (cnx->dso && cnx->egl.eglStreamConsumerAcquireKHR) {
1669         result = cnx->egl.eglStreamConsumerAcquireKHR(
1670                 dp->disp.dpy, stream);
1671     }
1672     return result;
1673 }
1674
1675 EGLBoolean eglStreamConsumerReleaseKHR(EGLDisplay dpy,
1676         EGLStreamKHR stream)
1677 {
1678     clearError();
1679
1680     const egl_display_ptr dp = validate_display(dpy);
1681     if (!dp) return EGL_FALSE;
1682
1683     EGLBoolean result = EGL_FALSE;
1684     egl_connection_t* const cnx = &gEGLImpl;
1685     if (cnx->dso && cnx->egl.eglStreamConsumerReleaseKHR) {
1686         result = cnx->egl.eglStreamConsumerReleaseKHR(
1687                 dp->disp.dpy, stream);
1688     }
1689     return result;
1690 }
1691
1692 EGLNativeFileDescriptorKHR eglGetStreamFileDescriptorKHR(
1693         EGLDisplay dpy, EGLStreamKHR stream)
1694 {
1695     clearError();
1696
1697     const egl_display_ptr dp = validate_display(dpy);
1698     if (!dp) return EGL_NO_FILE_DESCRIPTOR_KHR;
1699
1700     EGLNativeFileDescriptorKHR result = EGL_NO_FILE_DESCRIPTOR_KHR;
1701     egl_connection_t* const cnx = &gEGLImpl;
1702     if (cnx->dso && cnx->egl.eglGetStreamFileDescriptorKHR) {
1703         result = cnx->egl.eglGetStreamFileDescriptorKHR(
1704                 dp->disp.dpy, stream);
1705     }
1706     return result;
1707 }
1708
1709 EGLStreamKHR eglCreateStreamFromFileDescriptorKHR(
1710         EGLDisplay dpy, EGLNativeFileDescriptorKHR file_descriptor)
1711 {
1712     clearError();
1713
1714     const egl_display_ptr dp = validate_display(dpy);
1715     if (!dp) return EGL_NO_STREAM_KHR;
1716
1717     EGLStreamKHR result = EGL_NO_STREAM_KHR;
1718     egl_connection_t* const cnx = &gEGLImpl;
1719     if (cnx->dso && cnx->egl.eglCreateStreamFromFileDescriptorKHR) {
1720         result = cnx->egl.eglCreateStreamFromFileDescriptorKHR(
1721                 dp->disp.dpy, file_descriptor);
1722     }
1723     return result;
1724 }
1725
1726 // ----------------------------------------------------------------------------
1727 // EGL_EGLEXT_VERSION 15
1728 // ----------------------------------------------------------------------------
1729
1730 EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) {
1731     clearError();
1732     const egl_display_ptr dp = validate_display(dpy);
1733     if (!dp) return EGL_FALSE;
1734     EGLint result = EGL_FALSE;
1735     egl_connection_t* const cnx = &gEGLImpl;
1736     if (cnx->dso && cnx->egl.eglWaitSyncKHR) {
1737         result = cnx->egl.eglWaitSyncKHR(dp->disp.dpy, sync, flags);
1738     }
1739     return result;
1740 }
1741
1742 // ----------------------------------------------------------------------------
1743 // ANDROID extensions
1744 // ----------------------------------------------------------------------------
1745
1746 EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync)
1747 {
1748     clearError();
1749
1750     const egl_display_ptr dp = validate_display(dpy);
1751     if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID;
1752
1753     EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID;
1754     egl_connection_t* const cnx = &gEGLImpl;
1755     if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) {
1756         result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync);
1757     }
1758     return result;
1759 }
1760
1761 EGLBoolean eglPresentationTimeANDROID(EGLDisplay dpy, EGLSurface surface,
1762         EGLnsecsANDROID time)
1763 {
1764     clearError();
1765
1766     const egl_display_ptr dp = validate_display(dpy);
1767     if (!dp) {
1768         return EGL_FALSE;
1769     }
1770
1771     SurfaceRef _s(dp.get(), surface);
1772     if (!_s.get()) {
1773         setError(EGL_BAD_SURFACE, EGL_FALSE);
1774         return EGL_FALSE;
1775     }
1776
1777     egl_surface_t const * const s = get_surface(surface);
1778     native_window_set_buffers_timestamp(s->win.get(), time);
1779
1780     return EGL_TRUE;
1781 }
1782
1783 EGLClientBuffer eglCreateNativeClientBufferANDROID(const EGLint *attrib_list)
1784 {
1785     clearError();
1786
1787     int usage = 0;
1788     uint32_t width = 0;
1789     uint32_t height = 0;
1790     uint32_t format = 0;
1791     uint32_t red_size = 0;
1792     uint32_t green_size = 0;
1793     uint32_t blue_size = 0;
1794     uint32_t alpha_size = 0;
1795
1796 #define GET_POSITIVE_VALUE(case_name, target) \
1797     case case_name: \
1798         if (value > 0) { \
1799             target = value; \
1800         } else { \
1801             return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0); \
1802         } \
1803         break
1804
1805     if (attrib_list) {
1806         while (*attrib_list != EGL_NONE) {
1807             GLint attr = *attrib_list++;
1808             GLint value = *attrib_list++;
1809             switch (attr) {
1810                 GET_POSITIVE_VALUE(EGL_WIDTH, width);
1811                 GET_POSITIVE_VALUE(EGL_HEIGHT, height);
1812                 GET_POSITIVE_VALUE(EGL_RED_SIZE, red_size);
1813                 GET_POSITIVE_VALUE(EGL_GREEN_SIZE, green_size);
1814                 GET_POSITIVE_VALUE(EGL_BLUE_SIZE, blue_size);
1815                 GET_POSITIVE_VALUE(EGL_ALPHA_SIZE, alpha_size);
1816                 case EGL_NATIVE_BUFFER_USAGE_ANDROID:
1817                     if (value & EGL_NATIVE_BUFFER_USAGE_PROTECTED_BIT_ANDROID) {
1818                         usage |= GRALLOC_USAGE_PROTECTED;
1819                     }
1820                     if (value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) {
1821                         usage |= GRALLOC_USAGE_HW_RENDER;
1822                     }
1823                     if (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID) {
1824                         usage |= GRALLOC_USAGE_HW_TEXTURE;
1825                     }
1826                     // The buffer must be used for either a texture or a
1827                     // renderbuffer.
1828                     if ((value & EGL_NATIVE_BUFFER_USAGE_RENDERBUFFER_BIT_ANDROID) &&
1829                         (value & EGL_NATIVE_BUFFER_USAGE_TEXTURE_BIT_ANDROID)) {
1830                         return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1831                     }
1832                     break;
1833                 default:
1834                     return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1835             }
1836         }
1837     }
1838 #undef GET_POSITIVE_VALUE
1839
1840     // Validate format.
1841     if (red_size == 8 && green_size == 8 && blue_size == 8) {
1842         if (alpha_size == 8) {
1843             format = HAL_PIXEL_FORMAT_RGBA_8888;
1844         } else {
1845             format = HAL_PIXEL_FORMAT_RGB_888;
1846         }
1847     } else if (red_size == 5 && green_size == 6 && blue_size == 5 &&
1848                alpha_size == 0) {
1849         format == HAL_PIXEL_FORMAT_RGB_565;
1850     } else {
1851         ALOGE("Invalid native pixel format { r=%d, g=%d, b=%d, a=%d }",
1852                 red_size, green_size, blue_size, alpha_size);
1853         return setError(EGL_BAD_PARAMETER, (EGLClientBuffer)0);
1854     }
1855
1856     GraphicBuffer* gBuffer = new GraphicBuffer(width, height, format, usage);
1857     const status_t err = gBuffer->initCheck();
1858     if (err != NO_ERROR) {
1859         ALOGE("Unable to create native buffer { w=%d, h=%d, f=%d, u=%#x }: %#x",
1860                 width, height, format, usage, err);
1861         // Destroy the buffer.
1862         sp<GraphicBuffer> holder(gBuffer);
1863         return setError(EGL_BAD_ALLOC, (EGLClientBuffer)0);
1864     }
1865     ALOGD("Created new native buffer %p { w=%d, h=%d, f=%d, u=%#x }",
1866             gBuffer, width, height, format, usage);
1867     return static_cast<EGLClientBuffer>(gBuffer->getNativeBuffer());
1868 }
1869
1870 // ----------------------------------------------------------------------------
1871 // NVIDIA extensions
1872 // ----------------------------------------------------------------------------
1873 EGLuint64NV eglGetSystemTimeFrequencyNV()
1874 {
1875     clearError();
1876
1877     if (egl_init_drivers() == EGL_FALSE) {
1878         return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1879     }
1880
1881     EGLuint64NV ret = 0;
1882     egl_connection_t* const cnx = &gEGLImpl;
1883
1884     if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) {
1885         return cnx->egl.eglGetSystemTimeFrequencyNV();
1886     }
1887
1888     return setErrorQuiet(EGL_BAD_DISPLAY, 0);
1889 }
1890
1891 EGLuint64NV eglGetSystemTimeNV()
1892 {
1893     clearError();
1894
1895     if (egl_init_drivers() == EGL_FALSE) {
1896         return setError(EGL_BAD_PARAMETER, EGL_FALSE);
1897     }
1898
1899     EGLuint64NV ret = 0;
1900     egl_connection_t* const cnx = &gEGLImpl;
1901
1902     if (cnx->dso && cnx->egl.eglGetSystemTimeNV) {
1903         return cnx->egl.eglGetSystemTimeNV();
1904     }
1905
1906     return setErrorQuiet(EGL_BAD_DISPLAY, 0);
1907 }
1908
1909 // ----------------------------------------------------------------------------
1910 // Partial update extension
1911 // ----------------------------------------------------------------------------
1912 EGLBoolean eglSetDamageRegionKHR(EGLDisplay dpy, EGLSurface surface,
1913         EGLint *rects, EGLint n_rects)
1914 {
1915     clearError();
1916
1917     const egl_display_ptr dp = validate_display(dpy);
1918     if (!dp) {
1919         setError(EGL_BAD_DISPLAY, EGL_FALSE);
1920         return EGL_FALSE;
1921     }
1922
1923     SurfaceRef _s(dp.get(), surface);
1924     if (!_s.get()) {
1925         setError(EGL_BAD_SURFACE, EGL_FALSE);
1926         return EGL_FALSE;
1927     }
1928
1929     egl_surface_t const * const s = get_surface(surface);
1930     if (s->cnx->egl.eglSetDamageRegionKHR) {
1931         return s->cnx->egl.eglSetDamageRegionKHR(dp->disp.dpy, s->surface,
1932                 rects, n_rects);
1933     }
1934
1935     return EGL_FALSE;
1936 }