OSDN Git Service

Merge remote-tracking branch 'goog/stage-aosp-master' into HEAD
[android-x86/device-generic-goldfish-opengl.git] / system / egl / egl.cpp
1 /*
2 * Copyright (C) 2011 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 #include <assert.h>
18 #include "HostConnection.h"
19 #include "ThreadInfo.h"
20 #include "eglDisplay.h"
21 #include "eglSync.h"
22 #include "egl_ftable.h"
23 #include <cutils/log.h>
24 #include <cutils/properties.h>
25 #include "goldfish_sync.h"
26 #include "gralloc_cb.h"
27 #include "GLClientState.h"
28 #include "GLSharedGroup.h"
29 #include "eglContext.h"
30 #include "ClientAPIExts.h"
31 #include "EGLImage.h"
32 #include "ProcessPipe.h"
33
34 #include "GLEncoder.h"
35 #ifdef WITH_GLES2
36 #include "GL2Encoder.h"
37 #endif
38
39 #if PLATFORM_SDK_VERSION >= 16
40 #include <system/window.h>
41 #else // PLATFORM_SDK_VERSION >= 16
42 #include <private/ui/android_natives_priv.h>
43 #endif // PLATFORM_SDK_VERSION >= 16
44
45 #if PLATFORM_SDK_VERSION <= 16
46 #define queueBuffer_DEPRECATED queueBuffer
47 #define dequeueBuffer_DEPRECATED dequeueBuffer
48 #define cancelBuffer_DEPRECATED cancelBuffer
49 #endif // PLATFORM_SDK_VERSION <= 16
50
51 #define DEBUG_EGL 0
52
53 #if DEBUG_EGL
54 #define DPRINT(fmt,...) ALOGD("%s: " fmt, __FUNCTION__, ##__VA_ARGS__);
55 #else
56 #define DPRINT(...)
57 #endif
58
59 template<typename T>
60 static T setErrorFunc(GLint error, T returnValue) {
61     getEGLThreadInfo()->eglError = error;
62     return returnValue;
63 }
64
65 const char *  eglStrError(EGLint err)
66 {
67     switch (err){
68         case EGL_SUCCESS:           return "EGL_SUCCESS";
69         case EGL_NOT_INITIALIZED:   return "EGL_NOT_INITIALIZED";
70         case EGL_BAD_ACCESS:        return "EGL_BAD_ACCESS";
71         case EGL_BAD_ALLOC:         return "EGL_BAD_ALLOC";
72         case EGL_BAD_ATTRIBUTE:     return "EGL_BAD_ATTRIBUTE";
73         case EGL_BAD_CONFIG:        return "EGL_BAD_CONFIG";
74         case EGL_BAD_CONTEXT:       return "EGL_BAD_CONTEXT";
75         case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
76         case EGL_BAD_DISPLAY:       return "EGL_BAD_DISPLAY";
77         case EGL_BAD_MATCH:         return "EGL_BAD_MATCH";
78         case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
79         case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
80         case EGL_BAD_PARAMETER:     return "EGL_BAD_PARAMETER";
81         case EGL_BAD_SURFACE:       return "EGL_BAD_SURFACE";
82         case EGL_CONTEXT_LOST:      return "EGL_CONTEXT_LOST";
83         default: return "UNKNOWN";
84     }
85 }
86
87 #define LOG_EGL_ERRORS 1
88
89 #ifdef LOG_EGL_ERRORS
90
91 #define setErrorReturn(error, retVal)     \
92     {                                                \
93         ALOGE("tid %d: %s(%d): error 0x%x (%s)", gettid(), __FUNCTION__, __LINE__, error, eglStrError(error));     \
94         return setErrorFunc(error, retVal);            \
95     }
96
97 #define RETURN_ERROR(ret,err)           \
98     ALOGE("tid %d: %s(%d): error 0x%x (%s)", gettid(), __FUNCTION__, __LINE__, err, eglStrError(err));    \
99     getEGLThreadInfo()->eglError = err;    \
100     return ret;
101
102 #else //!LOG_EGL_ERRORS
103
104 #define setErrorReturn(error, retVal) return setErrorFunc(error, retVal);
105
106 #define RETURN_ERROR(ret,err)           \
107     getEGLThreadInfo()->eglError = err; \
108     return ret;
109
110 #endif //LOG_EGL_ERRORS
111
112 #define VALIDATE_CONFIG(cfg,ret) \
113     if(((intptr_t)(cfg)<0)||((intptr_t)(cfg)>s_display.getNumConfigs())) { \
114         RETURN_ERROR(ret,EGL_BAD_CONFIG); \
115     }
116
117 #define VALIDATE_DISPLAY(dpy,ret) \
118     if ((dpy) != (EGLDisplay)&s_display) { \
119         RETURN_ERROR(ret, EGL_BAD_DISPLAY);    \
120     }
121
122 #define VALIDATE_DISPLAY_INIT(dpy,ret) \
123     VALIDATE_DISPLAY(dpy, ret)    \
124     if (!s_display.initialized()) {        \
125         RETURN_ERROR(ret, EGL_NOT_INITIALIZED);    \
126     }
127
128 #define DEFINE_HOST_CONNECTION \
129     HostConnection *hostCon = HostConnection::get(); \
130     ExtendedRCEncoderContext *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL)
131
132 #define DEFINE_AND_VALIDATE_HOST_CONNECTION(ret) \
133     HostConnection *hostCon = HostConnection::get(); \
134     if (!hostCon) { \
135         ALOGE("egl: Failed to get host connection\n"); \
136         return ret; \
137     } \
138     ExtendedRCEncoderContext *rcEnc = hostCon->rcEncoder(); \
139     if (!rcEnc) { \
140         ALOGE("egl: Failed to get renderControl encoder context\n"); \
141         return ret; \
142     }
143
144 #define VALIDATE_CONTEXT_RETURN(context,ret)  \
145     if (!(context)) {                         \
146         RETURN_ERROR(ret,EGL_BAD_CONTEXT);    \
147     }
148
149 #define VALIDATE_SURFACE_RETURN(surface, ret)    \
150     if ((surface) != EGL_NO_SURFACE) {    \
151         egl_surface_t* s( static_cast<egl_surface_t*>(surface) );    \
152         if (s->dpy != (EGLDisplay)&s_display)    \
153             setErrorReturn(EGL_BAD_DISPLAY, EGL_FALSE);    \
154     }
155
156 // The one and only supported display object.
157 static eglDisplay s_display;
158
159 EGLContext_t::EGLContext_t(EGLDisplay dpy, EGLConfig config, EGLContext_t* shareCtx) :
160     dpy(dpy),
161     config(config),
162     read(EGL_NO_SURFACE),
163     draw(EGL_NO_SURFACE),
164     shareCtx(shareCtx),
165     rcContext(0),
166     versionString(NULL),
167     vendorString(NULL),
168     rendererString(NULL),
169     shaderVersionString(NULL),
170     extensionString(NULL),
171     deletePending(0),
172     goldfishSyncFd(-1)
173 {
174     flags = 0;
175     version = 1;
176     clientState = new GLClientState();
177     if (shareCtx)
178         sharedGroup = shareCtx->getSharedGroup();
179     else
180         sharedGroup = GLSharedGroupPtr(new GLSharedGroup());
181     assert(dpy == (EGLDisplay)&s_display);
182     s_display.onCreateContext((EGLContext)this);
183 };
184
185 int EGLContext_t::getGoldfishSyncFd() {
186     if (goldfishSyncFd < 0) {
187         goldfishSyncFd = goldfish_sync_open();
188     }
189     return goldfishSyncFd;
190 }
191
192 EGLContext_t::~EGLContext_t()
193 {
194     if (goldfishSyncFd > 0) {
195         goldfish_sync_close(goldfishSyncFd);
196         goldfishSyncFd = -1;
197     }
198     assert(dpy == (EGLDisplay)&s_display);
199     s_display.onDestroyContext((EGLContext)this);
200     delete clientState;
201     delete [] versionString;
202     delete [] vendorString;
203     delete [] rendererString;
204     delete [] shaderVersionString;
205     delete [] extensionString;
206 }
207
208 // ----------------------------------------------------------------------------
209 //egl_surface_t
210
211 //we don't need to handle depth since it's handled when window created on the host
212
213 struct egl_surface_t {
214
215     EGLDisplay          dpy;
216     EGLConfig           config;
217
218
219     egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLint surfaceType);
220     virtual     ~egl_surface_t();
221
222     virtual     void        setSwapInterval(int interval) = 0;
223     virtual     EGLBoolean  swapBuffers() = 0;
224
225     EGLint      getSwapBehavior() const;
226     uint32_t    getRcSurface()   { return rcSurface; }
227     EGLint      getSurfaceType() { return surfaceType; }
228
229     EGLint      getWidth(){ return width; }
230     EGLint      getHeight(){ return height; }
231     EGLint      getNativeWidth(){ return nativeWidth; }
232     EGLint      getNativeHeight(){ return nativeHeight; }
233     void        setTextureFormat(EGLint _texFormat) { texFormat = _texFormat; }
234     EGLint      getTextureFormat() { return texFormat; }
235     void        setTextureTarget(EGLint _texTarget) { texTarget = _texTarget; }
236     EGLint      getTextureTarget() { return texTarget; }
237
238 private:
239     //
240     //Surface attributes
241     //
242     EGLint      width;
243     EGLint      height;
244     EGLint      texFormat;
245     EGLint      texTarget;
246
247     // Width of the actual window being presented (not the EGL texture)
248     // Give it some default values.
249     int nativeWidth;
250     int nativeHeight;
251
252 protected:
253     void        setWidth(EGLint w)  { width = w;  }
254     void        setHeight(EGLint h) { height = h; }
255     void        setNativeWidth(int w)  { nativeWidth = w;  }
256     void        setNativeHeight(int h) { nativeHeight = h; }
257
258     EGLint      surfaceType;
259     uint32_t    rcSurface; //handle to surface created via remote control
260 };
261
262 egl_surface_t::egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLint surfaceType)
263     : dpy(dpy), config(config), surfaceType(surfaceType), rcSurface(0)
264 {
265     width = 0;
266     height = 0;
267     // prevent div by 0 in EGL_(HORIZONTAL|VERTICAL)_RESOLUTION queries.
268     nativeWidth = 1;
269     nativeHeight = 1;
270     texFormat = EGL_NO_TEXTURE;
271     texTarget = EGL_NO_TEXTURE;
272     assert(dpy == (EGLDisplay)&s_display);
273     s_display.onCreateSurface((EGLSurface)this);
274 }
275
276 EGLint egl_surface_t::getSwapBehavior() const {
277     return EGL_BUFFER_PRESERVED;
278 }
279
280 egl_surface_t::~egl_surface_t()
281 {
282     assert(dpy == (EGLDisplay)&s_display);
283     s_display.onDestroySurface((EGLSurface)this);
284 }
285
286 // ----------------------------------------------------------------------------
287 // egl_window_surface_t
288
289 struct egl_window_surface_t : public egl_surface_t {
290     static egl_window_surface_t* create(
291             EGLDisplay dpy, EGLConfig config, EGLint surfType,
292             ANativeWindow* window);
293
294     virtual ~egl_window_surface_t();
295
296     virtual void       setSwapInterval(int interval);
297     virtual EGLBoolean swapBuffers();
298
299 private:
300     egl_window_surface_t(
301             EGLDisplay dpy, EGLConfig config, EGLint surfType,
302             ANativeWindow* window);
303     EGLBoolean init();
304
305     ANativeWindow*              nativeWindow;
306     android_native_buffer_t*    buffer;
307 };
308
309 egl_window_surface_t::egl_window_surface_t (
310         EGLDisplay dpy, EGLConfig config, EGLint surfType,
311         ANativeWindow* window)
312 :   egl_surface_t(dpy, config, surfType),
313     nativeWindow(window),
314     buffer(NULL)
315 {
316     // keep a reference on the window
317     nativeWindow->common.incRef(&nativeWindow->common);
318 }
319
320
321 EGLBoolean egl_window_surface_t::init()
322 {
323     if (nativeWindow->dequeueBuffer_DEPRECATED(nativeWindow, &buffer) != NO_ERROR) {
324         setErrorReturn(EGL_BAD_ALLOC, EGL_FALSE);
325     }
326     setWidth(buffer->width);
327     setHeight(buffer->height);
328
329     int nativeWidth, nativeHeight;
330           nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &nativeWidth);
331           nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &nativeHeight);
332
333     setNativeWidth(nativeWidth);
334     setNativeHeight(nativeHeight);
335
336     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
337     rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uintptr_t)config,
338             getWidth(), getHeight());
339     if (!rcSurface) {
340         ALOGE("rcCreateWindowSurface returned 0");
341         return EGL_FALSE;
342     }
343     rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface,
344             ((cb_handle_t*)(buffer->handle))->hostHandle);
345
346     return EGL_TRUE;
347 }
348
349 egl_window_surface_t* egl_window_surface_t::create(
350         EGLDisplay dpy, EGLConfig config, EGLint surfType,
351         ANativeWindow* window)
352 {
353     egl_window_surface_t* wnd = new egl_window_surface_t(
354             dpy, config, surfType, window);
355     if (wnd && !wnd->init()) {
356         delete wnd;
357         wnd = NULL;
358     }
359     return wnd;
360 }
361
362 egl_window_surface_t::~egl_window_surface_t() {
363     DEFINE_HOST_CONNECTION;
364     if (rcSurface && rcEnc) {
365         rcEnc->rcDestroyWindowSurface(rcEnc, rcSurface);
366     }
367     if (buffer) {
368         nativeWindow->cancelBuffer_DEPRECATED(nativeWindow, buffer);
369     }
370     nativeWindow->common.decRef(&nativeWindow->common);
371 }
372
373 void egl_window_surface_t::setSwapInterval(int interval)
374 {
375     nativeWindow->setSwapInterval(nativeWindow, interval);
376 }
377
378 // createNativeSync() creates an OpenGL sync object on the host
379 // using rcCreateSyncKHR. If necessary, a native fence FD will
380 // also be created through the goldfish sync device.
381 // Returns a handle to the host-side FenceSync object.
382 static uint64_t createNativeSync(EGLenum type,
383                                  const EGLint* attrib_list,
384                                  int num_actual_attribs,
385                                  bool destroy_when_signaled,
386                                  int fd_in,
387                                  int* fd_out) {
388     DEFINE_HOST_CONNECTION;
389
390     uint64_t sync_handle;
391     uint64_t thread_handle;
392
393     EGLint* actual_attribs =
394         (EGLint*)(num_actual_attribs == 0 ? NULL : attrib_list);
395
396     rcEnc->rcCreateSyncKHR(rcEnc, type,
397                            actual_attribs,
398                            num_actual_attribs * sizeof(EGLint),
399                            destroy_when_signaled,
400                            &sync_handle,
401                            &thread_handle);
402
403     if (type == EGL_SYNC_NATIVE_FENCE_ANDROID && fd_in < 0) {
404         int queue_work_err =
405             goldfish_sync_queue_work(
406                     getEGLThreadInfo()->currentContext->getGoldfishSyncFd(),
407                     sync_handle,
408                     thread_handle,
409                     fd_out);
410
411         DPRINT("got native fence fd=%d queue_work_err=%d",
412                *fd_out, queue_work_err);
413     }
414
415     return sync_handle;
416 }
417
418 // createGoldfishOpenGLNativeSync() is for creating host-only sync objects
419 // that are needed by only this goldfish opengl driver,
420 // such as in swapBuffers().
421 // The guest will not see any of these, and these sync objects will be
422 // destroyed on the host when signaled.
423 // A native fence FD is possibly returned.
424 static void createGoldfishOpenGLNativeSync(int* fd_out) {
425     createNativeSync(EGL_SYNC_NATIVE_FENCE_ANDROID,
426                      NULL /* empty attrib list */,
427                      0 /* 0 attrib count */,
428                      true /* destroy when signaled. this is host-only
429                              and there will only be one waiter */,
430                      -1 /* we want a new fd */,
431                      fd_out);
432 }
433
434 EGLBoolean egl_window_surface_t::swapBuffers()
435 {
436
437     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
438
439     // Follow up flushWindowColorBuffer with a fence command.
440     // When the fence command finishes,
441     // we're sure that the buffer on the host
442     // has been blitted.
443     //
444     // |presentFenceFd| guards the presentation of the
445     // current frame with a goldfish sync fence fd.
446     //
447     // When |presentFenceFd| is signaled, the recipient
448     // of the buffer that was sent through queueBuffer
449     // can be sure that the buffer is current.
450     //
451     // If we don't take care of this synchronization,
452     // an old frame can be processed by surfaceflinger,
453     // resulting in out of order frames.
454
455     int presentFenceFd = -1;
456
457 #if PLATFORM_SDK_VERSION <= 16
458     rcEnc->rcFlushWindowColorBuffer(rcEnc, rcSurface);
459     // equivalent to glFinish if no native sync
460     eglWaitClient();
461     nativeWindow->queueBuffer(nativeWindow, buffer);
462 #else
463     if (rcEnc->hasNativeSync()) {
464         rcEnc->rcFlushWindowColorBufferAsync(rcEnc, rcSurface);
465         createGoldfishOpenGLNativeSync(&presentFenceFd);
466     } else {
467         rcEnc->rcFlushWindowColorBuffer(rcEnc, rcSurface);
468         // equivalent to glFinish if no native sync
469         eglWaitClient();
470     }
471
472     DPRINT("queueBuffer with fence %d", presentFenceFd);
473     nativeWindow->queueBuffer(nativeWindow, buffer, presentFenceFd);
474 #endif
475
476     DPRINT("calling dequeueBuffer...");
477
478 #if PLATFORM_SDK_VERSION <= 16
479     if (nativeWindow->dequeueBuffer(nativeWindow, &buffer)) {
480         buffer = NULL;
481         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
482     }
483 #else
484     int acquireFenceFd = -1;
485     if (nativeWindow->dequeueBuffer(nativeWindow, &buffer, &acquireFenceFd)) {
486         buffer = NULL;
487         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
488     }
489
490     DPRINT("dequeueBuffer with fence %d", acquireFenceFd);
491
492     if (acquireFenceFd > 0) {
493         close(acquireFenceFd);
494     }
495 #endif
496
497     rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface,
498             ((cb_handle_t *)(buffer->handle))->hostHandle);
499
500     setWidth(buffer->width);
501     setHeight(buffer->height);
502
503     return EGL_TRUE;
504 }
505
506 // ----------------------------------------------------------------------------
507 //egl_pbuffer_surface_t
508
509 struct egl_pbuffer_surface_t : public egl_surface_t {
510     static egl_pbuffer_surface_t* create(EGLDisplay dpy, EGLConfig config,
511             EGLint surfType, int32_t w, int32_t h, GLenum pixelFormat);
512
513     virtual ~egl_pbuffer_surface_t();
514
515     virtual void       setSwapInterval(int interval) { (void)interval; }
516     virtual EGLBoolean swapBuffers() { return EGL_TRUE; }
517
518     uint32_t getRcColorBuffer() { return rcColorBuffer; }
519
520 private:
521     egl_pbuffer_surface_t(EGLDisplay dpy, EGLConfig config, EGLint surfType,
522             int32_t w, int32_t h);
523     EGLBoolean init(GLenum format);
524
525     uint32_t rcColorBuffer;
526 };
527
528 egl_pbuffer_surface_t::egl_pbuffer_surface_t(EGLDisplay dpy, EGLConfig config,
529         EGLint surfType, int32_t w, int32_t h)
530 :   egl_surface_t(dpy, config, surfType),
531     rcColorBuffer(0)
532 {
533     setWidth(w);
534     setHeight(h);
535 }
536
537 egl_pbuffer_surface_t::~egl_pbuffer_surface_t()
538 {
539     DEFINE_HOST_CONNECTION;
540     if (rcEnc) {
541         if (rcColorBuffer) rcEnc->rcCloseColorBuffer(rcEnc, rcColorBuffer);
542         if (rcSurface)     rcEnc->rcDestroyWindowSurface(rcEnc, rcSurface);
543     }
544 }
545
546 EGLBoolean egl_pbuffer_surface_t::init(GLenum pixelFormat)
547 {
548     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
549
550     rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uintptr_t)config,
551             getWidth(), getHeight());
552     if (!rcSurface) {
553         ALOGE("rcCreateWindowSurface returned 0");
554         return EGL_FALSE;
555     }
556
557     rcColorBuffer = rcEnc->rcCreateColorBuffer(rcEnc, getWidth(), getHeight(),
558             pixelFormat);
559     if (!rcColorBuffer) {
560         ALOGE("rcCreateColorBuffer returned 0");
561         return EGL_FALSE;
562     }
563
564     rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface, rcColorBuffer);
565
566     return EGL_TRUE;
567 }
568
569 egl_pbuffer_surface_t* egl_pbuffer_surface_t::create(EGLDisplay dpy,
570         EGLConfig config, EGLint surfType, int32_t w, int32_t h,
571         GLenum pixelFormat)
572 {
573     egl_pbuffer_surface_t* pb = new egl_pbuffer_surface_t(dpy, config, surfType,
574             w, h);
575     if (pb && !pb->init(pixelFormat)) {
576         delete pb;
577         pb = NULL;
578     }
579     return pb;
580 }
581
582 static const char *getGLString(int glEnum)
583 {
584     EGLThreadInfo *tInfo = getEGLThreadInfo();
585     if (!tInfo || !tInfo->currentContext) {
586         return NULL;
587     }
588
589     const char** strPtr = NULL;
590
591 #define GL_VENDOR                         0x1F00
592 #define GL_RENDERER                       0x1F01
593 #define GL_VERSION                        0x1F02
594 #define GL_SHADING_LANGUAGE_VERSION       0x8B8C
595 #define GL_EXTENSIONS                     0x1F03
596
597     switch(glEnum) {
598         case GL_VERSION:
599             strPtr = &tInfo->currentContext->versionString;
600             break;
601         case GL_VENDOR:
602             strPtr = &tInfo->currentContext->vendorString;
603             break;
604         case GL_RENDERER:
605             strPtr = &tInfo->currentContext->rendererString;
606             break;
607         case GL_SHADING_LANGUAGE_VERSION:
608             strPtr = &tInfo->currentContext->shaderVersionString;
609             break;
610         case GL_EXTENSIONS:
611             strPtr = &tInfo->currentContext->extensionString;
612             break;
613     }
614
615     if (!strPtr) {
616         return NULL;
617     }
618
619     if (*strPtr != NULL) {
620         //
621         // string is already cached
622         //
623         return *strPtr;
624     }
625
626     //
627     // first query of that string - need to query host
628     //
629     DEFINE_AND_VALIDATE_HOST_CONNECTION(NULL);
630     char *hostStr = NULL;
631     int n = rcEnc->rcGetGLString(rcEnc, glEnum, NULL, 0);
632     if (n < 0) {
633         hostStr = new char[-n+1];
634         n = rcEnc->rcGetGLString(rcEnc, glEnum, hostStr, -n);
635         if (n <= 0) {
636             delete [] hostStr;
637             hostStr = NULL;
638         }
639     }
640
641     //
642     // keep the string in the context and return its value
643     //
644     *strPtr = hostStr;
645     return hostStr;
646 }
647
648 // ----------------------------------------------------------------------------
649
650 static EGLClient_eglInterface s_eglIface = {
651     getThreadInfo: getEGLThreadInfo,
652     getGLString: getGLString
653 };
654
655 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
656 EGLDisplay eglGetDisplay(EGLNativeDisplayType display_id)
657 {
658     //
659     // we support only EGL_DEFAULT_DISPLAY.
660     //
661     if (display_id != EGL_DEFAULT_DISPLAY) {
662         return EGL_NO_DISPLAY;
663     }
664
665     return (EGLDisplay)&s_display;
666 }
667
668 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
669 {
670     VALIDATE_DISPLAY(dpy,EGL_FALSE);
671
672     if (!s_display.initialize(&s_eglIface)) {
673         return EGL_FALSE;
674     }
675     if (major!=NULL)
676         *major = s_display.getVersionMajor();
677     if (minor!=NULL)
678         *minor = s_display.getVersionMinor();
679     return EGL_TRUE;
680 }
681
682 EGLBoolean eglTerminate(EGLDisplay dpy)
683 {
684     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
685
686     s_display.terminate();
687     return EGL_TRUE;
688 }
689
690 EGLint eglGetError()
691 {
692     EGLint error = getEGLThreadInfo()->eglError;
693     getEGLThreadInfo()->eglError = EGL_SUCCESS;
694     return error;
695 }
696
697 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
698 {
699     // search in EGL function table
700     for (int i=0; i<egl_num_funcs; i++) {
701         if (!strcmp(egl_funcs_by_name[i].name, procname)) {
702             return (__eglMustCastToProperFunctionPointerType)egl_funcs_by_name[i].proc;
703         }
704     }
705
706     // look in gles client api's extensions table
707     return (__eglMustCastToProperFunctionPointerType)ClientAPIExts::getProcAddress(procname);
708
709     // Fail - function not found.
710     return NULL;
711 }
712
713 const char* eglQueryString(EGLDisplay dpy, EGLint name)
714 {
715     VALIDATE_DISPLAY_INIT(dpy, NULL);
716
717     return s_display.queryString(name);
718 }
719
720 EGLBoolean eglGetConfigs(EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config)
721 {
722     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
723
724     if(!num_config) {
725         RETURN_ERROR(EGL_FALSE,EGL_BAD_PARAMETER);
726     }
727
728     GLint numConfigs = s_display.getNumConfigs();
729     if (!configs) {
730         *num_config = numConfigs;
731         return EGL_TRUE;
732     }
733
734     EGLint i;
735     for (i = 0 ; i < numConfigs && i < config_size ; i++) {
736         *configs++ = (EGLConfig)(uintptr_t)i;
737     }
738     *num_config = i;
739     return EGL_TRUE;
740 }
741
742 EGLBoolean eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
743 {
744     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
745
746     if (!num_config) {
747         setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
748     }
749
750     int attribs_size = 0;
751     if (attrib_list) {
752         const EGLint * attrib_p = attrib_list;
753         while (attrib_p[0] != EGL_NONE) {
754             attribs_size += 2;
755             attrib_p += 2;
756         }
757         attribs_size++; //for the terminating EGL_NONE
758     }
759
760     // API 19 passes EGL_SWAP_BEHAVIOR_PRESERVED_BIT to surface type,
761     // while the host never supports it.
762     // We remove the bit here.
763     EGLint* local_attrib_list = NULL;
764     if (PLATFORM_SDK_VERSION <= 19) {
765         local_attrib_list = new EGLint[attribs_size];
766         memcpy(local_attrib_list, attrib_list, attribs_size * sizeof(EGLint));
767         EGLint* local_attrib_p = local_attrib_list;
768         while (local_attrib_p[0] != EGL_NONE) {
769             if (local_attrib_p[0] == EGL_SURFACE_TYPE) {
770                 local_attrib_p[1] &= ~(EGLint)EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
771             }
772             local_attrib_p += 2;
773         }
774     }
775
776     uint32_t* tempConfigs[config_size];
777     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
778     *num_config = rcEnc->rcChooseConfig(rcEnc,
779             local_attrib_list ? local_attrib_list:(EGLint*)attrib_list,
780             attribs_size * sizeof(EGLint), (uint32_t*)tempConfigs, config_size);
781
782     if (local_attrib_list) delete [] local_attrib_list;
783     if (*num_config <= 0) {
784         EGLint err = -(*num_config);
785         *num_config = 0;
786         switch (err) {
787             case EGL_BAD_ATTRIBUTE:
788                 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
789             default:
790                 return EGL_FALSE;
791         }
792     }
793
794     if (configs!=NULL) {
795         EGLint i=0;
796         for (i=0;i<(*num_config);i++) {
797              *((uintptr_t*)configs+i) = *((uint32_t*)tempConfigs+i);
798         }
799     }
800
801     return EGL_TRUE;
802 }
803
804 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
805 {
806     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
807     VALIDATE_CONFIG(config, EGL_FALSE);
808
809     if (s_display.getConfigAttrib(config, attribute, value))
810     {
811         return EGL_TRUE;
812     }
813     else
814     {
815         RETURN_ERROR(EGL_FALSE, EGL_BAD_ATTRIBUTE);
816     }
817 }
818
819 EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list)
820 {
821     (void)attrib_list;
822
823     VALIDATE_DISPLAY_INIT(dpy, NULL);
824     VALIDATE_CONFIG(config, EGL_FALSE);
825     if (win == 0) {
826         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
827     }
828
829     EGLint surfaceType;
830     if (s_display.getConfigAttrib(config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)    return EGL_FALSE;
831
832     if (!(surfaceType & EGL_WINDOW_BIT)) {
833         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
834     }
835
836     if (static_cast<ANativeWindow*>(win)->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
837         setErrorReturn(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
838     }
839
840     egl_surface_t* surface = egl_window_surface_t::create(
841             &s_display, config, EGL_WINDOW_BIT, static_cast<ANativeWindow*>(win));
842     if (!surface) {
843         setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
844     }
845
846     return surface;
847 }
848
849 EGLSurface eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
850 {
851     VALIDATE_DISPLAY_INIT(dpy, NULL);
852     VALIDATE_CONFIG(config, EGL_FALSE);
853
854     EGLint surfaceType;
855     if (s_display.getConfigAttrib(config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)    return EGL_FALSE;
856
857     if (!(surfaceType & EGL_PBUFFER_BIT)) {
858         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
859     }
860
861     int32_t w = 0;
862     int32_t h = 0;
863     EGLint texFormat = EGL_NO_TEXTURE;
864     EGLint texTarget = EGL_NO_TEXTURE;
865     while (attrib_list[0] != EGL_NONE) {
866         switch (attrib_list[0]) {
867             case EGL_WIDTH:
868                 w = attrib_list[1];
869                 if (w < 0) setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
870                 break;
871             case EGL_HEIGHT:
872                 h = attrib_list[1];
873                 if (h < 0) setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
874                 break;
875             case EGL_TEXTURE_FORMAT:
876                 texFormat = attrib_list[1];
877                 break;
878             case EGL_TEXTURE_TARGET:
879                 texTarget = attrib_list[1];
880                 break;
881             // the followings are not supported
882             case EGL_LARGEST_PBUFFER:
883             case EGL_MIPMAP_TEXTURE:
884             case EGL_VG_ALPHA_FORMAT:
885             case EGL_VG_COLORSPACE:
886                 break;
887             default:
888                 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
889         };
890         attrib_list+=2;
891     }
892     if (((texFormat == EGL_NO_TEXTURE)&&(texTarget != EGL_NO_TEXTURE)) ||
893         ((texFormat != EGL_NO_TEXTURE)&&(texTarget == EGL_NO_TEXTURE))) {
894         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
895     }
896     // TODO: check EGL_TEXTURE_FORMAT - need to support eglBindTexImage
897
898     GLenum pixelFormat;
899     if (s_display.getConfigGLPixelFormat(config, &pixelFormat) == EGL_FALSE)
900         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
901
902     egl_surface_t* surface = egl_pbuffer_surface_t::create(dpy, config,
903             EGL_PBUFFER_BIT, w, h, pixelFormat);
904     if (!surface) {
905         setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
906     }
907
908     //setup attributes
909     surface->setTextureFormat(texFormat);
910     surface->setTextureTarget(texTarget);
911
912     return surface;
913 }
914
915 EGLSurface eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint *attrib_list)
916 {
917     //XXX: Pixmap not supported. The host cannot render to a pixmap resource
918     //     located on host. In order to support Pixmaps we should either punt
919     //     to s/w rendering -or- let the host render to a buffer that will be
920     //     copied back to guest at some sync point. None of those methods not
921     //     implemented and pixmaps are not used with OpenGL anyway ...
922     VALIDATE_CONFIG(config, EGL_FALSE);
923     (void)dpy;
924     (void)pixmap;
925     (void)attrib_list;
926     return EGL_NO_SURFACE;
927 }
928
929 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
930 {
931     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
932     VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
933
934     egl_surface_t* surface(static_cast<egl_surface_t*>(eglSurface));
935     delete surface;
936
937     return EGL_TRUE;
938 }
939
940 static float s_getNativeDpi() {
941     float nativeDPI = 560.0f;
942     const char* dpiPropName = "qemu.sf.lcd_density";
943     char dpiProp[PROPERTY_VALUE_MAX];
944     if (property_get(dpiPropName, dpiProp, NULL) > 0) {
945         nativeDPI = atof(dpiProp);
946     }
947     return nativeDPI;
948 }
949
950 EGLBoolean eglQuerySurface(EGLDisplay dpy, EGLSurface eglSurface, EGLint attribute, EGLint *value)
951 {
952     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
953     VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
954
955     egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
956
957     // Parameters involved in queries of EGL_(HORIZONTAL|VERTICAL)_RESOLUTION
958     float currWidth, currHeight, scaledResolution, effectiveSurfaceDPI;
959     EGLBoolean ret = EGL_TRUE;
960     switch (attribute) {
961         case EGL_CONFIG_ID:
962             ret = s_display.getConfigAttrib(surface->config, EGL_CONFIG_ID, value);
963             break;
964         case EGL_WIDTH:
965             *value = surface->getWidth();
966             break;
967         case EGL_HEIGHT:
968             *value = surface->getHeight();
969             break;
970         case EGL_TEXTURE_FORMAT:
971             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) {
972                 *value = surface->getTextureFormat();
973             }
974             break;
975         case EGL_TEXTURE_TARGET:
976             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) {
977                 *value = surface->getTextureTarget();
978             }
979             break;
980         case EGL_SWAP_BEHAVIOR:
981         {
982             EGLint surfaceType;
983             ret = s_display.getConfigAttrib(surface->config, EGL_SURFACE_TYPE,
984                     &surfaceType);
985             if (ret == EGL_TRUE) {
986                 if (surfaceType & EGL_SWAP_BEHAVIOR_PRESERVED_BIT) {
987                     *value = EGL_BUFFER_PRESERVED;
988                 } else {
989                     *value = EGL_BUFFER_DESTROYED;
990                 }
991             }
992             break;
993         }
994         case EGL_LARGEST_PBUFFER:
995             // not modified for a window or pixmap surface
996             // and we ignore it when creating a PBuffer surface (default is EGL_FALSE)
997             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) *value = EGL_FALSE;
998             break;
999         case EGL_MIPMAP_TEXTURE:
1000             // not modified for a window or pixmap surface
1001             // and we ignore it when creating a PBuffer surface (default is 0)
1002             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) *value = false;
1003             break;
1004         case EGL_MIPMAP_LEVEL:
1005             // not modified for a window or pixmap surface
1006             // and we ignore it when creating a PBuffer surface (default is 0)
1007             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) *value = 0;
1008             break;
1009         case EGL_MULTISAMPLE_RESOLVE:
1010             // ignored when creating the surface, return default
1011             *value = EGL_MULTISAMPLE_RESOLVE_DEFAULT;
1012             break;
1013         case EGL_HORIZONTAL_RESOLUTION:
1014             // pixel/mm * EGL_DISPLAY_SCALING
1015             // TODO: get the DPI from avd config
1016             currWidth = surface->getWidth();
1017             scaledResolution = currWidth / surface->getNativeWidth();
1018             effectiveSurfaceDPI =
1019                 scaledResolution * s_getNativeDpi() * EGL_DISPLAY_SCALING;
1020             *value = (EGLint)(effectiveSurfaceDPI);
1021             break;
1022         case EGL_VERTICAL_RESOLUTION:
1023             // pixel/mm * EGL_DISPLAY_SCALING
1024             // TODO: get the real DPI from avd config
1025             currHeight = surface->getHeight();
1026             scaledResolution = currHeight / surface->getNativeHeight();
1027             effectiveSurfaceDPI =
1028                 scaledResolution * s_getNativeDpi() * EGL_DISPLAY_SCALING;
1029             *value = (EGLint)(effectiveSurfaceDPI);
1030             break;
1031         case EGL_PIXEL_ASPECT_RATIO:
1032             // w / h * EGL_DISPLAY_SCALING
1033             // Please don't ask why * EGL_DISPLAY_SCALING, the document says it
1034             *value = 1 * EGL_DISPLAY_SCALING;
1035             break;
1036         case EGL_RENDER_BUFFER:
1037             switch (surface->getSurfaceType()) {
1038                 case EGL_PBUFFER_BIT:
1039                     *value = EGL_BACK_BUFFER;
1040                     break;
1041                 case EGL_PIXMAP_BIT:
1042                     *value = EGL_SINGLE_BUFFER;
1043                     break;
1044                 case EGL_WINDOW_BIT:
1045                     // ignored when creating the surface, return default
1046                     *value = EGL_BACK_BUFFER;
1047                     break;
1048                 default:
1049                     ALOGE("eglQuerySurface %x unknown surface type %x",
1050                             attribute, surface->getSurfaceType());
1051                     ret = setErrorFunc(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1052                     break;
1053             }
1054             break;
1055         case EGL_VG_COLORSPACE:
1056             // ignored when creating the surface, return default
1057             *value = EGL_VG_COLORSPACE_sRGB;
1058             break;
1059         case EGL_VG_ALPHA_FORMAT:
1060             // ignored when creating the surface, return default
1061             *value = EGL_VG_ALPHA_FORMAT_NONPRE;
1062             break;
1063         //TODO: complete other attributes
1064         default:
1065             ALOGE("eglQuerySurface %x  EGL_BAD_ATTRIBUTE", attribute);
1066             ret = setErrorFunc(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1067             break;
1068     }
1069
1070     return ret;
1071 }
1072
1073 EGLBoolean eglBindAPI(EGLenum api)
1074 {
1075     if (api != EGL_OPENGL_ES_API)
1076         setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1077     return EGL_TRUE;
1078 }
1079
1080 EGLenum eglQueryAPI()
1081 {
1082     return EGL_OPENGL_ES_API;
1083 }
1084
1085 EGLBoolean eglWaitClient()
1086 {
1087     return eglWaitGL();
1088 }
1089
1090 EGLBoolean eglReleaseThread()
1091 {
1092     EGLThreadInfo *tInfo = getEGLThreadInfo();
1093     if (tInfo) {
1094         tInfo->eglError = EGL_SUCCESS;
1095         EGLContext_t* context = tInfo->currentContext;
1096         if (context) {
1097             // The following code is doing pretty much the same thing as
1098             // eglMakeCurrent(&s_display, EGL_NO_CONTEXT, EGL_NO_SURFACE, EGL_NO_SURFACE)
1099             // with the only issue that we do not require a valid display here.
1100             DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1101             rcEnc->rcMakeCurrent(rcEnc, 0, 0, 0);
1102              if (context->version == 2) {
1103                 hostCon->gl2Encoder()->setClientState(NULL);
1104                 hostCon->gl2Encoder()->setSharedGroup(GLSharedGroupPtr());
1105             }
1106             else {
1107                 hostCon->glEncoder()->setClientState(NULL);
1108                 hostCon->glEncoder()->setSharedGroup(GLSharedGroupPtr());
1109             }
1110             context->flags &= ~EGLContext_t::IS_CURRENT;
1111
1112             if (context->deletePending) {
1113                 if (context->rcContext) {
1114                     rcEnc->rcDestroyContext(rcEnc, context->rcContext);
1115                     context->rcContext = 0;
1116                 }
1117                 delete context;
1118             }
1119             tInfo->currentContext = 0;
1120         }
1121     }
1122     return EGL_TRUE;
1123 }
1124
1125 EGLSurface eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint *attrib_list)
1126 {
1127     //TODO
1128     (void)dpy;
1129     (void)buftype;
1130     (void)buffer;
1131     (void)config;
1132     (void)attrib_list;
1133     ALOGW("%s not implemented", __FUNCTION__);
1134     return 0;
1135 }
1136
1137 EGLBoolean eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1138 {
1139     // Right now we don't do anything when using host GPU.
1140     // This is purely just to pass the data through
1141     // without issuing a warning. We may benefit from validating the
1142     // display and surface for debug purposes.
1143     // TODO: Find cases where we actually need to do something.
1144     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1145     VALIDATE_SURFACE_RETURN(surface, EGL_FALSE);
1146     if (surface == EGL_NO_SURFACE) {
1147         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1148     }
1149
1150     (void)value;
1151
1152     egl_surface_t* p_surface( static_cast<egl_surface_t*>(surface) );
1153     switch (attribute) {
1154     case EGL_MIPMAP_LEVEL:
1155         return true;
1156         break;
1157     case EGL_MULTISAMPLE_RESOLVE:
1158     {
1159         if (value == EGL_MULTISAMPLE_RESOLVE_BOX) {
1160             EGLint surface_type;
1161             s_display.getConfigAttrib(p_surface->config, EGL_SURFACE_TYPE, &surface_type);
1162             if (0 == (surface_type & EGL_MULTISAMPLE_RESOLVE_BOX_BIT)) {
1163                 setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1164             }
1165         }
1166         return true;
1167         break;
1168     }
1169     case EGL_SWAP_BEHAVIOR:
1170         if (value == EGL_BUFFER_PRESERVED) {
1171             EGLint surface_type;
1172             s_display.getConfigAttrib(p_surface->config, EGL_SURFACE_TYPE, &surface_type);
1173             if (0 == (surface_type & EGL_SWAP_BEHAVIOR_PRESERVED_BIT)) {
1174                 setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1175             }
1176         }
1177         return true;
1178         break;
1179     default:
1180         ALOGW("%s: attr=0x%x not implemented", __FUNCTION__, attribute);
1181         setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1182     }
1183     return false;
1184 }
1185
1186 EGLBoolean eglBindTexImage(EGLDisplay dpy, EGLSurface eglSurface, EGLint buffer)
1187 {
1188     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1189     VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
1190     if (eglSurface == EGL_NO_SURFACE) {
1191         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1192     }
1193
1194     if (buffer != EGL_BACK_BUFFER) {
1195         setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1196     }
1197
1198     egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
1199
1200     if (surface->getTextureFormat() == EGL_NO_TEXTURE) {
1201         setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1202     }
1203
1204     if (!(surface->getSurfaceType() & EGL_PBUFFER_BIT)) {
1205         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1206     }
1207
1208     //It's now safe to cast to pbuffer surface
1209     egl_pbuffer_surface_t* pbSurface = (egl_pbuffer_surface_t*)surface;
1210
1211     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1212     rcEnc->rcBindTexture(rcEnc, pbSurface->getRcColorBuffer());
1213
1214     return GL_TRUE;
1215 }
1216
1217 EGLBoolean eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1218 {
1219     //TODO
1220     (void)dpy;
1221     (void)surface;
1222     (void)buffer;
1223     ALOGW("%s not implemented", __FUNCTION__);
1224     return 0;
1225 }
1226
1227 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1228 {
1229     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1230     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1231
1232     EGLContext_t* ctx = getEGLThreadInfo()->currentContext;
1233     if (!ctx) {
1234         setErrorReturn(EGL_BAD_CONTEXT, EGL_FALSE);
1235     }
1236     if (!ctx->draw) {
1237         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1238     }
1239     egl_surface_t* draw(static_cast<egl_surface_t*>(ctx->draw));
1240     draw->setSwapInterval(interval);
1241
1242     rcEnc->rcFBSetSwapInterval(rcEnc, interval); //TODO: implement on the host
1243
1244     return EGL_TRUE;
1245 }
1246
1247 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list)
1248 {
1249     VALIDATE_DISPLAY_INIT(dpy, EGL_NO_CONTEXT);
1250     VALIDATE_CONFIG(config, EGL_NO_CONTEXT);
1251
1252     EGLint version = 1; //default
1253     while (attrib_list && attrib_list[0] != EGL_NONE) {
1254         if (attrib_list[0] == EGL_CONTEXT_CLIENT_VERSION) {
1255             version = attrib_list[1];
1256         } else { // Only the attribute EGL_CONTEXT_CLIENT_VERSION may be specified.
1257             setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_CONTEXT);
1258         }
1259         attrib_list+=2;
1260     }
1261
1262     // Currently only support GLES1 and 2
1263     if (version != 1 && version != 2) {
1264         setErrorReturn(EGL_BAD_CONFIG, EGL_NO_CONTEXT);
1265     }
1266
1267     uint32_t rcShareCtx = 0;
1268     EGLContext_t * shareCtx = NULL;
1269     if (share_context) {
1270         shareCtx = static_cast<EGLContext_t*>(share_context);
1271         rcShareCtx = shareCtx->rcContext;
1272         if (shareCtx->dpy != dpy)
1273             setErrorReturn(EGL_BAD_MATCH, EGL_NO_CONTEXT);
1274     }
1275
1276     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_NO_CONTEXT);
1277     // We've created EGL context. Disconnecting
1278     // would be dangerous at this point.
1279     hostCon->setGrallocOnly(false);
1280
1281     uint32_t rcContext = rcEnc->rcCreateContext(rcEnc, (uintptr_t)config, rcShareCtx, version);
1282     if (!rcContext) {
1283         ALOGE("rcCreateContext returned 0");
1284         setErrorReturn(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1285     }
1286
1287     EGLContext_t * context = new EGLContext_t(dpy, config, shareCtx);
1288     if (!context)
1289         setErrorReturn(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1290
1291     context->version = version;
1292     context->rcContext = rcContext;
1293
1294
1295     return context;
1296 }
1297
1298 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1299 {
1300     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1301     VALIDATE_CONTEXT_RETURN(ctx, EGL_FALSE);
1302
1303     EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
1304
1305     if (!context) return EGL_TRUE;
1306
1307     if (getEGLThreadInfo()->currentContext == context) {
1308         getEGLThreadInfo()->currentContext->deletePending = 1;
1309         return EGL_TRUE;
1310     }
1311
1312     if (context->rcContext) {
1313         DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1314         rcEnc->rcDestroyContext(rcEnc, context->rcContext);
1315         context->rcContext = 0;
1316     }
1317
1318     delete context;
1319     return EGL_TRUE;
1320 }
1321
1322 EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
1323 {
1324     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1325     VALIDATE_SURFACE_RETURN(draw, EGL_FALSE);
1326     VALIDATE_SURFACE_RETURN(read, EGL_FALSE);
1327
1328     if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1329         setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1330     if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1331         setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1332
1333     EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
1334     uint32_t ctxHandle = (context) ? context->rcContext : 0;
1335     egl_surface_t * drawSurf = static_cast<egl_surface_t *>(draw);
1336     uint32_t drawHandle = (drawSurf) ? drawSurf->getRcSurface() : 0;
1337     egl_surface_t * readSurf = static_cast<egl_surface_t *>(read);
1338     uint32_t readHandle = (readSurf) ? readSurf->getRcSurface() : 0;
1339
1340     //
1341     // Nothing to do if no binding change has made
1342     //
1343     EGLThreadInfo *tInfo = getEGLThreadInfo();
1344
1345     if (tInfo->currentContext == context &&
1346         (context == NULL ||
1347         (context && context->draw == draw && context->read == read))) {
1348         return EGL_TRUE;
1349     }
1350
1351     if (tInfo->currentContext && tInfo->currentContext->deletePending) {
1352         if (tInfo->currentContext != context) {
1353             EGLContext_t * contextToDelete = tInfo->currentContext;
1354             tInfo->currentContext = 0;
1355             eglDestroyContext(dpy, contextToDelete);
1356         }
1357     }
1358
1359     if (context && (context->flags & EGLContext_t::IS_CURRENT) && (context != tInfo->currentContext)) {
1360         //context is current to another thread
1361         setErrorReturn(EGL_BAD_ACCESS, EGL_FALSE);
1362     }
1363
1364     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1365     if (rcEnc->rcMakeCurrent(rcEnc, ctxHandle, drawHandle, readHandle) == EGL_FALSE) {
1366         ALOGE("rcMakeCurrent returned EGL_FALSE");
1367         setErrorReturn(EGL_BAD_CONTEXT, EGL_FALSE);
1368     }
1369
1370     //Now make the local bind
1371     if (context) {
1372         // This is a nontrivial context.
1373         // The thread cannot be gralloc-only anymore.
1374         hostCon->setGrallocOnly(false);
1375         context->draw = draw;
1376         context->read = read;
1377         context->flags |= EGLContext_t::IS_CURRENT;
1378         //set the client state
1379         if (context->version == 2) {
1380             hostCon->gl2Encoder()->setClientStateMakeCurrent(context->getClientState());
1381             hostCon->gl2Encoder()->setSharedGroup(context->getSharedGroup());
1382         }
1383         else {
1384             hostCon->glEncoder()->setClientState(context->getClientState());
1385             hostCon->glEncoder()->setSharedGroup(context->getSharedGroup());
1386         }
1387     }
1388     else if (tInfo->currentContext) {
1389         //release ClientState & SharedGroup
1390         if (tInfo->currentContext->version == 2) {
1391             hostCon->gl2Encoder()->setClientState(NULL);
1392             hostCon->gl2Encoder()->setSharedGroup(GLSharedGroupPtr(NULL));
1393         }
1394         else {
1395             hostCon->glEncoder()->setClientState(NULL);
1396             hostCon->glEncoder()->setSharedGroup(GLSharedGroupPtr(NULL));
1397         }
1398
1399     }
1400
1401     if (tInfo->currentContext)
1402         tInfo->currentContext->flags &= ~EGLContext_t::IS_CURRENT;
1403
1404     //Now make current
1405     tInfo->currentContext = context;
1406
1407     //Check maybe we need to init the encoder, if it's first eglMakeCurrent
1408     if (tInfo->currentContext) {
1409         if (tInfo->currentContext->version == 2) {
1410             if (!hostCon->gl2Encoder()->isInitialized()) {
1411                 s_display.gles2_iface()->init();
1412                 hostCon->gl2Encoder()->setInitialized();
1413                 ClientAPIExts::initClientFuncs(s_display.gles2_iface(), 1);
1414             }
1415         }
1416         else {
1417             if (!hostCon->glEncoder()->isInitialized()) {
1418                 s_display.gles_iface()->init();
1419                 hostCon->glEncoder()->setInitialized();
1420                 ClientAPIExts::initClientFuncs(s_display.gles_iface(), 0);
1421             }
1422         }
1423     }
1424
1425     return EGL_TRUE;
1426 }
1427
1428 EGLContext eglGetCurrentContext()
1429 {
1430     return getEGLThreadInfo()->currentContext;
1431 }
1432
1433 EGLSurface eglGetCurrentSurface(EGLint readdraw)
1434 {
1435     EGLContext_t * context = getEGLThreadInfo()->currentContext;
1436     if (!context)
1437         return EGL_NO_SURFACE; //not an error
1438
1439     switch (readdraw) {
1440         case EGL_READ:
1441             return context->read;
1442         case EGL_DRAW:
1443             return context->draw;
1444         default:
1445             setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1446     }
1447 }
1448
1449 EGLDisplay eglGetCurrentDisplay()
1450 {
1451     EGLContext_t * context = getEGLThreadInfo()->currentContext;
1452     if (!context)
1453         return EGL_NO_DISPLAY; //not an error
1454
1455     return context->dpy;
1456 }
1457
1458 EGLBoolean eglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value)
1459 {
1460     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1461     VALIDATE_CONTEXT_RETURN(ctx, EGL_FALSE);
1462
1463     EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
1464
1465     EGLBoolean ret = EGL_TRUE;
1466     switch (attribute) {
1467         case EGL_CONFIG_ID:
1468             ret = s_display.getConfigAttrib(context->config, EGL_CONFIG_ID, value);
1469             break;
1470         case EGL_CONTEXT_CLIENT_TYPE:
1471             *value = EGL_OPENGL_ES_API;
1472             break;
1473         case EGL_CONTEXT_CLIENT_VERSION:
1474             *value = context->version;
1475             break;
1476         case EGL_RENDER_BUFFER:
1477             if (!context->draw)
1478                 *value = EGL_NONE;
1479             else
1480                 *value = EGL_BACK_BUFFER; //single buffer not supported
1481             break;
1482         default:
1483             ALOGE("eglQueryContext %x  EGL_BAD_ATTRIBUTE", attribute);
1484             setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1485             break;
1486     }
1487
1488     return ret;
1489 }
1490
1491 EGLBoolean eglWaitGL()
1492 {
1493     EGLThreadInfo *tInfo = getEGLThreadInfo();
1494     if (!tInfo || !tInfo->currentContext) {
1495         return EGL_FALSE;
1496     }
1497
1498     if (tInfo->currentContext->version == 2) {
1499         s_display.gles2_iface()->finish();
1500     }
1501     else {
1502         s_display.gles_iface()->finish();
1503     }
1504
1505     return EGL_TRUE;
1506 }
1507
1508 EGLBoolean eglWaitNative(EGLint engine)
1509 {
1510     (void)engine;
1511     return EGL_TRUE;
1512 }
1513
1514 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface eglSurface)
1515 {
1516     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1517     if (eglSurface == EGL_NO_SURFACE)
1518         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1519
1520     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1521
1522     egl_surface_t* d = static_cast<egl_surface_t*>(eglSurface);
1523     if (d->dpy != dpy)
1524         setErrorReturn(EGL_BAD_DISPLAY, EGL_FALSE);
1525
1526     // post the surface
1527     d->swapBuffers();
1528
1529     hostCon->flush();
1530     return EGL_TRUE;
1531 }
1532
1533 EGLBoolean eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1534 {
1535     //TODO :later
1536     (void)dpy;
1537     (void)surface;
1538     (void)target;
1539     return 0;
1540 }
1541
1542 EGLBoolean eglLockSurfaceKHR(EGLDisplay display, EGLSurface surface, const EGLint *attrib_list)
1543 {
1544     //TODO later
1545     (void)display;
1546     (void)surface;
1547     (void)attrib_list;
1548     return 0;
1549 }
1550
1551 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay display, EGLSurface surface)
1552 {
1553     //TODO later
1554     (void)display;
1555     (void)surface;
1556     return 0;
1557 }
1558
1559 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list)
1560 {
1561     (void)attrib_list;
1562
1563     VALIDATE_DISPLAY_INIT(dpy, EGL_NO_IMAGE_KHR);
1564
1565     if (target == EGL_NATIVE_BUFFER_ANDROID) {
1566         if (ctx != EGL_NO_CONTEXT) {
1567             setErrorReturn(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1568         }
1569
1570         android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
1571
1572         if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
1573             setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1574
1575         if (native_buffer->common.version != sizeof(android_native_buffer_t))
1576             setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1577
1578         cb_handle_t *cb = (cb_handle_t *)(native_buffer->handle);
1579
1580         switch (cb->format) {
1581             case HAL_PIXEL_FORMAT_RGBA_8888:
1582             case HAL_PIXEL_FORMAT_RGBX_8888:
1583             case HAL_PIXEL_FORMAT_RGB_888:
1584             case HAL_PIXEL_FORMAT_RGB_565:
1585             case HAL_PIXEL_FORMAT_YV12:
1586             case HAL_PIXEL_FORMAT_BGRA_8888:
1587                 break;
1588             default:
1589                 setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1590         }
1591
1592         native_buffer->common.incRef(&native_buffer->common);
1593
1594         EGLImage_t *image = new EGLImage_t();
1595         image->dpy = dpy;
1596         image->target = target;
1597         image->native_buffer = native_buffer;
1598
1599         return (EGLImageKHR)image;
1600     }
1601     else if (target == EGL_GL_TEXTURE_2D_KHR) {
1602         VALIDATE_CONTEXT_RETURN(ctx, EGL_NO_IMAGE_KHR);
1603
1604         EGLContext_t *context = static_cast<EGLContext_t*>(ctx);
1605         DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_NO_IMAGE_KHR);
1606
1607         uint32_t ctxHandle = (context) ? context->rcContext : 0;
1608         GLuint texture = (GLuint)reinterpret_cast<uintptr_t>(buffer);
1609         uint32_t img = rcEnc->rcCreateClientImage(rcEnc, ctxHandle, target, texture);
1610         EGLImage_t *image = new EGLImage_t();
1611         image->dpy = dpy;
1612         image->target = target;
1613         image->host_egl_image = img;
1614
1615         return (EGLImageKHR)image;
1616     }
1617     
1618     setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1619 }
1620
1621 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1622 {
1623     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1624     EGLImage_t *image = (EGLImage_t*)img;
1625
1626     if (!image || image->dpy != dpy) {
1627         RETURN_ERROR(EGL_FALSE, EGL_BAD_PARAMETER);
1628     }
1629
1630     if (image->target == EGL_NATIVE_BUFFER_ANDROID) {
1631         android_native_buffer_t* native_buffer = image->native_buffer;
1632
1633         if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
1634             setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1635
1636         if (native_buffer->common.version != sizeof(android_native_buffer_t))
1637             setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1638
1639         native_buffer->common.decRef(&native_buffer->common);
1640         delete image;
1641
1642         return EGL_TRUE;
1643     }
1644     else if (image->target == EGL_GL_TEXTURE_2D_KHR) {
1645         uint32_t host_egl_image = image->host_egl_image;
1646         delete image;
1647         DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1648         return rcEnc->rcDestroyClientImage(rcEnc, host_egl_image);
1649     }
1650
1651     setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1652 }
1653
1654 #define FENCE_SYNC_HANDLE (EGLSyncKHR)0xFE4CE
1655 #define MAX_EGL_SYNC_ATTRIBS 10
1656
1657 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
1658         const EGLint *attrib_list)
1659 {
1660     VALIDATE_DISPLAY(dpy, EGL_NO_SYNC_KHR);
1661     DPRINT("type for eglCreateSyncKHR: 0x%x", type);
1662
1663     DEFINE_HOST_CONNECTION;
1664
1665     if ((type != EGL_SYNC_FENCE_KHR &&
1666          type != EGL_SYNC_NATIVE_FENCE_ANDROID) ||
1667         (type != EGL_SYNC_FENCE_KHR &&
1668          !rcEnc->hasNativeSync())) {
1669         setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
1670     }
1671
1672     EGLThreadInfo *tInfo = getEGLThreadInfo();
1673     if (!tInfo || !tInfo->currentContext) {
1674         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1675     }
1676
1677     int num_actual_attribs = 0;
1678
1679     // If attrib_list is not NULL,
1680     // ensure attrib_list contains (key, value) pairs
1681     // followed by a single EGL_NONE.
1682     // Also validate attribs.
1683     int inputFenceFd = -1;
1684     if (attrib_list) {
1685         for (int i = 0; i < MAX_EGL_SYNC_ATTRIBS; i += 2) {
1686             if (attrib_list[i] == EGL_NONE) {
1687                 num_actual_attribs = i;
1688                 break;
1689             }
1690             if (i + 1 == MAX_EGL_SYNC_ATTRIBS) {
1691                 DPRINT("ERROR: attrib list without EGL_NONE");
1692                 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
1693             }
1694         }
1695
1696         // Validate and input attribs
1697         for (int i = 0; i < num_actual_attribs; i += 2) {
1698             if (attrib_list[i] == EGL_SYNC_TYPE_KHR) {
1699                 DPRINT("ERROR: attrib key = EGL_SYNC_TYPE_KHR");
1700             }
1701             if (attrib_list[i] == EGL_SYNC_STATUS_KHR) {
1702                 DPRINT("ERROR: attrib key = EGL_SYNC_STATUS_KHR");
1703             }
1704             if (attrib_list[i] == EGL_SYNC_CONDITION_KHR) {
1705                 DPRINT("ERROR: attrib key = EGL_SYNC_CONDITION_KHR");
1706             }
1707             EGLint attrib_key = attrib_list[i];
1708             EGLint attrib_val = attrib_list[i + 1];
1709             if (attrib_key == EGL_SYNC_NATIVE_FENCE_FD_ANDROID) {
1710                 if (attrib_val != EGL_NO_NATIVE_FENCE_FD_ANDROID) {
1711                     inputFenceFd = attrib_val;
1712                 }
1713             }
1714             DPRINT("attrib: 0x%x : 0x%x", attrib_key, attrib_val);
1715         }
1716     }
1717
1718     uint64_t sync_handle = 0;
1719     int newFenceFd = -1;
1720
1721     if (rcEnc->hasNativeSync()) {
1722         sync_handle =
1723             createNativeSync(type, attrib_list, num_actual_attribs,
1724                              false /* don't destroy when signaled on the host;
1725                                       let the guest clean this up,
1726                                       because the guest called eglCreateSyncKHR. */,
1727                              inputFenceFd,
1728                              &newFenceFd);
1729
1730     } else {
1731         // Just trigger a glFinish if the native sync on host
1732         // is unavailable.
1733         eglWaitClient();
1734     }
1735
1736     EGLSync_t* syncRes = new EGLSync_t(sync_handle);
1737
1738     if (type == EGL_SYNC_NATIVE_FENCE_ANDROID) {
1739         syncRes->type = EGL_SYNC_NATIVE_FENCE_ANDROID;
1740
1741         if (inputFenceFd < 0) {
1742             syncRes->android_native_fence_fd = newFenceFd;
1743         } else {
1744             DPRINT("has input fence fd %d",
1745                     inputFenceFd);
1746             syncRes->android_native_fence_fd = inputFenceFd;
1747         }
1748     } else {
1749         syncRes->type = EGL_SYNC_FENCE_KHR;
1750         syncRes->android_native_fence_fd = -1;
1751         if (!rcEnc->hasNativeSync()) {
1752             syncRes->status = EGL_SIGNALED_KHR;
1753         }
1754     }
1755
1756     return (EGLSyncKHR)syncRes;
1757 }
1758
1759 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR eglsync)
1760 {
1761     (void)dpy;
1762
1763     if (!eglsync) {
1764         DPRINT("WARNING: null sync object")
1765         return EGL_TRUE;
1766     }
1767
1768     EGLSync_t* sync = static_cast<EGLSync_t*>(eglsync);
1769
1770     if (sync && sync->android_native_fence_fd > 0) {
1771         close(sync->android_native_fence_fd);
1772         sync->android_native_fence_fd = -1;
1773     }
1774
1775     if (sync) {
1776         DEFINE_HOST_CONNECTION;
1777         if (rcEnc->hasNativeSync()) {
1778             rcEnc->rcDestroySyncKHR(rcEnc, sync->handle);
1779         }
1780         delete sync;
1781     }
1782
1783     return EGL_TRUE;
1784 }
1785
1786 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR eglsync, EGLint flags,
1787         EGLTimeKHR timeout)
1788 {
1789     (void)dpy;
1790
1791     if (!eglsync) {
1792         DPRINT("WARNING: null sync object");
1793         return EGL_CONDITION_SATISFIED_KHR;
1794     }
1795
1796     EGLSync_t* sync = (EGLSync_t*)eglsync;
1797
1798     DPRINT("sync=0x%lx (handle=0x%lx) flags=0x%x timeout=0x%llx",
1799            sync, sync->handle, flags, timeout);
1800
1801     DEFINE_HOST_CONNECTION;
1802
1803     EGLint retval;
1804     if (rcEnc->hasNativeSync()) {
1805         retval = rcEnc->rcClientWaitSyncKHR
1806             (rcEnc, sync->handle, flags, timeout);
1807     } else {
1808         retval = EGL_CONDITION_SATISFIED_KHR;
1809     }
1810     EGLint res_status;
1811     switch (sync->type) {
1812         case EGL_SYNC_FENCE_KHR:
1813             res_status = EGL_SIGNALED_KHR;
1814             break;
1815         case EGL_SYNC_NATIVE_FENCE_ANDROID:
1816             res_status = EGL_SYNC_NATIVE_FENCE_SIGNALED_ANDROID;
1817             break;
1818         default:
1819             res_status = EGL_SIGNALED_KHR;
1820     }
1821     sync->status = res_status;
1822     return retval;
1823 }
1824
1825 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR eglsync,
1826         EGLint attribute, EGLint *value)
1827 {
1828     (void)dpy;
1829
1830     EGLSync_t* sync = (EGLSync_t*)eglsync;
1831
1832     switch (attribute) {
1833     case EGL_SYNC_TYPE_KHR:
1834         *value = sync->type;
1835         return EGL_TRUE;
1836     case EGL_SYNC_STATUS_KHR:
1837         *value = sync->status;
1838         return EGL_TRUE;
1839     case EGL_SYNC_CONDITION_KHR:
1840         *value = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
1841         return EGL_TRUE;
1842     default:
1843         setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1844     }
1845 }
1846
1847 int eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR eglsync) {
1848     (void)dpy;
1849
1850     DPRINT("call");
1851
1852     EGLSync_t* sync = (EGLSync_t*)eglsync;
1853     if (sync && sync->android_native_fence_fd > 0) {
1854         int res = dup(sync->android_native_fence_fd);
1855         return res;
1856     } else {
1857         return -1;
1858     }
1859 }
1860
1861 // TODO: Implement EGL_KHR_wait_sync
1862 EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR eglsync, EGLint flags) {
1863     (void)dpy;
1864     (void)eglsync;
1865     (void)flags;
1866     return EGL_TRUE;
1867 }