OSDN Git Service

Merge "GL2Encoder: Do not forward GLES API version queries"
[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(), pixelFormat);
558     if (!rcColorBuffer) {
559         ALOGE("rcCreateColorBuffer returned 0");
560         return EGL_FALSE;
561     }
562
563     rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface, rcColorBuffer);
564
565     return EGL_TRUE;
566 }
567
568 egl_pbuffer_surface_t* egl_pbuffer_surface_t::create(EGLDisplay dpy,
569         EGLConfig config, EGLint surfType, int32_t w, int32_t h,
570         GLenum pixelFormat)
571 {
572     egl_pbuffer_surface_t* pb = new egl_pbuffer_surface_t(dpy, config, surfType,
573             w, h);
574     if (pb && !pb->init(pixelFormat)) {
575         delete pb;
576         pb = NULL;
577     }
578     return pb;
579 }
580
581 static const char *getGLString(int glEnum)
582 {
583     EGLThreadInfo *tInfo = getEGLThreadInfo();
584     if (!tInfo || !tInfo->currentContext) {
585         return NULL;
586     }
587
588     const char** strPtr = NULL;
589
590 #define GL_VENDOR                         0x1F00
591 #define GL_RENDERER                       0x1F01
592 #define GL_VERSION                        0x1F02
593 #define GL_SHADING_LANGUAGE_VERSION       0x8B8C
594 #define GL_EXTENSIONS                     0x1F03
595
596     switch(glEnum) {
597         case GL_VERSION:
598             strPtr = &tInfo->currentContext->versionString;
599             break;
600         case GL_VENDOR:
601             strPtr = &tInfo->currentContext->vendorString;
602             break;
603         case GL_RENDERER:
604             strPtr = &tInfo->currentContext->rendererString;
605             break;
606         case GL_SHADING_LANGUAGE_VERSION:
607             strPtr = &tInfo->currentContext->shaderVersionString;
608             break;
609         case GL_EXTENSIONS:
610             strPtr = &tInfo->currentContext->extensionString;
611             break;
612     }
613
614     if (!strPtr) {
615         return NULL;
616     }
617
618     if (*strPtr != NULL) {
619         //
620         // string is already cached
621         //
622         return *strPtr;
623     }
624
625     //
626     // first query of that string - need to query host
627     //
628     DEFINE_AND_VALIDATE_HOST_CONNECTION(NULL);
629     char *hostStr = NULL;
630     int n = rcEnc->rcGetGLString(rcEnc, glEnum, NULL, 0);
631     if (n < 0) {
632         hostStr = new char[-n+1];
633         n = rcEnc->rcGetGLString(rcEnc, glEnum, hostStr, -n);
634         if (n <= 0) {
635             delete [] hostStr;
636             hostStr = NULL;
637         }
638     }
639
640     //
641     // keep the string in the context and return its value
642     //
643     *strPtr = hostStr;
644     return hostStr;
645 }
646
647 // ----------------------------------------------------------------------------
648
649 static EGLClient_eglInterface s_eglIface = {
650     getThreadInfo: getEGLThreadInfo,
651     getGLString: getGLString
652 };
653
654 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
655 EGLDisplay eglGetDisplay(EGLNativeDisplayType display_id)
656 {
657     //
658     // we support only EGL_DEFAULT_DISPLAY.
659     //
660     if (display_id != EGL_DEFAULT_DISPLAY) {
661         return EGL_NO_DISPLAY;
662     }
663
664     return (EGLDisplay)&s_display;
665 }
666
667 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
668 {
669     VALIDATE_DISPLAY(dpy,EGL_FALSE);
670
671     if (!s_display.initialize(&s_eglIface)) {
672         return EGL_FALSE;
673     }
674     if (major!=NULL)
675         *major = s_display.getVersionMajor();
676     if (minor!=NULL)
677         *minor = s_display.getVersionMinor();
678     return EGL_TRUE;
679 }
680
681 EGLBoolean eglTerminate(EGLDisplay dpy)
682 {
683     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
684
685     s_display.terminate();
686     return EGL_TRUE;
687 }
688
689 EGLint eglGetError()
690 {
691     EGLint error = getEGLThreadInfo()->eglError;
692     getEGLThreadInfo()->eglError = EGL_SUCCESS;
693     return error;
694 }
695
696 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
697 {
698     // search in EGL function table
699     for (int i=0; i<egl_num_funcs; i++) {
700         if (!strcmp(egl_funcs_by_name[i].name, procname)) {
701             return (__eglMustCastToProperFunctionPointerType)egl_funcs_by_name[i].proc;
702         }
703     }
704
705     // look in gles client api's extensions table
706     return (__eglMustCastToProperFunctionPointerType)ClientAPIExts::getProcAddress(procname);
707
708     // Fail - function not found.
709     return NULL;
710 }
711
712 const char* eglQueryString(EGLDisplay dpy, EGLint name)
713 {
714     VALIDATE_DISPLAY_INIT(dpy, NULL);
715
716     return s_display.queryString(name);
717 }
718
719 EGLBoolean eglGetConfigs(EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config)
720 {
721     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
722
723     if(!num_config) {
724         RETURN_ERROR(EGL_FALSE,EGL_BAD_PARAMETER);
725     }
726
727     GLint numConfigs = s_display.getNumConfigs();
728     if (!configs) {
729         *num_config = numConfigs;
730         return EGL_TRUE;
731     }
732
733     EGLint i;
734     for (i = 0 ; i < numConfigs && i < config_size ; i++) {
735         *configs++ = (EGLConfig)(uintptr_t)i;
736     }
737     *num_config = i;
738     return EGL_TRUE;
739 }
740
741 EGLBoolean eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
742 {
743     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
744
745     if (!num_config) {
746         setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
747     }
748
749     int attribs_size = 0;
750     if (attrib_list) {
751         const EGLint * attrib_p = attrib_list;
752         while (attrib_p[0] != EGL_NONE) {
753             attribs_size += 2;
754             attrib_p += 2;
755         }
756         attribs_size++; //for the terminating EGL_NONE
757     }
758
759     // API 19 passes EGL_SWAP_BEHAVIOR_PRESERVED_BIT to surface type,
760     // while the host never supports it.
761     // We remove the bit here.
762     EGLint* local_attrib_list = NULL;
763     if (PLATFORM_SDK_VERSION <= 19) {
764         local_attrib_list = new EGLint[attribs_size];
765         memcpy(local_attrib_list, attrib_list, attribs_size * sizeof(EGLint));
766         EGLint* local_attrib_p = local_attrib_list;
767         while (local_attrib_p[0] != EGL_NONE) {
768             if (local_attrib_p[0] == EGL_SURFACE_TYPE) {
769                 local_attrib_p[1] &= ~(EGLint)EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
770             }
771             local_attrib_p += 2;
772         }
773     }
774
775     uint32_t* tempConfigs[config_size];
776     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
777     *num_config = rcEnc->rcChooseConfig(rcEnc,
778             local_attrib_list ? local_attrib_list:(EGLint*)attrib_list,
779             attribs_size * sizeof(EGLint), (uint32_t*)tempConfigs, config_size);
780
781     if (local_attrib_list) delete [] local_attrib_list;
782     if (*num_config <= 0) {
783         EGLint err = -(*num_config);
784         *num_config = 0;
785         switch (err) {
786             case EGL_BAD_ATTRIBUTE:
787                 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
788             default:
789                 return EGL_FALSE;
790         }
791     }
792
793     if (configs!=NULL) {
794         EGLint i=0;
795         for (i=0;i<(*num_config);i++) {
796              *((uintptr_t*)configs+i) = *((uint32_t*)tempConfigs+i);
797         }
798     }
799
800     return EGL_TRUE;
801 }
802
803 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
804 {
805     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
806     VALIDATE_CONFIG(config, EGL_FALSE);
807
808     if (s_display.getConfigAttrib(config, attribute, value))
809     {
810         return EGL_TRUE;
811     }
812     else
813     {
814         RETURN_ERROR(EGL_FALSE, EGL_BAD_ATTRIBUTE);
815     }
816 }
817
818 EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list)
819 {
820     (void)attrib_list;
821
822     VALIDATE_DISPLAY_INIT(dpy, NULL);
823     VALIDATE_CONFIG(config, EGL_FALSE);
824     if (win == 0) {
825         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
826     }
827
828     EGLint surfaceType;
829     if (s_display.getConfigAttrib(config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)    return EGL_FALSE;
830
831     if (!(surfaceType & EGL_WINDOW_BIT)) {
832         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
833     }
834
835     if (static_cast<ANativeWindow*>(win)->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
836         setErrorReturn(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
837     }
838
839     egl_surface_t* surface = egl_window_surface_t::create(
840             &s_display, config, EGL_WINDOW_BIT, static_cast<ANativeWindow*>(win));
841     if (!surface) {
842         setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
843     }
844
845     return surface;
846 }
847
848 EGLSurface eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
849 {
850     VALIDATE_DISPLAY_INIT(dpy, NULL);
851     VALIDATE_CONFIG(config, EGL_FALSE);
852
853     EGLint surfaceType;
854     if (s_display.getConfigAttrib(config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE)    return EGL_FALSE;
855
856     if (!(surfaceType & EGL_PBUFFER_BIT)) {
857         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
858     }
859
860     int32_t w = 0;
861     int32_t h = 0;
862     EGLint texFormat = EGL_NO_TEXTURE;
863     EGLint texTarget = EGL_NO_TEXTURE;
864     while (attrib_list[0] != EGL_NONE) {
865         switch (attrib_list[0]) {
866             case EGL_WIDTH:
867                 w = attrib_list[1];
868                 if (w < 0) setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
869                 break;
870             case EGL_HEIGHT:
871                 h = attrib_list[1];
872                 if (h < 0) setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
873                 break;
874             case EGL_TEXTURE_FORMAT:
875                 texFormat = attrib_list[1];
876                 break;
877             case EGL_TEXTURE_TARGET:
878                 texTarget = attrib_list[1];
879                 break;
880             // the followings are not supported
881             case EGL_LARGEST_PBUFFER:
882             case EGL_MIPMAP_TEXTURE:
883             case EGL_VG_ALPHA_FORMAT:
884             case EGL_VG_COLORSPACE:
885                 break;
886             default:
887                 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
888         };
889         attrib_list+=2;
890     }
891     if (((texFormat == EGL_NO_TEXTURE)&&(texTarget != EGL_NO_TEXTURE)) ||
892         ((texFormat != EGL_NO_TEXTURE)&&(texTarget == EGL_NO_TEXTURE))) {
893         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
894     }
895     // TODO: check EGL_TEXTURE_FORMAT - need to support eglBindTexImage
896
897     GLenum pixelFormat;
898     if (s_display.getConfigGLPixelFormat(config, &pixelFormat) == EGL_FALSE)
899         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
900
901     egl_surface_t* surface = egl_pbuffer_surface_t::create(dpy, config,
902             EGL_PBUFFER_BIT, w, h, pixelFormat);
903     if (!surface) {
904         setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
905     }
906
907     //setup attributes
908     surface->setTextureFormat(texFormat);
909     surface->setTextureTarget(texTarget);
910
911     return surface;
912 }
913
914 EGLSurface eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint *attrib_list)
915 {
916     //XXX: Pixmap not supported. The host cannot render to a pixmap resource
917     //     located on host. In order to support Pixmaps we should either punt
918     //     to s/w rendering -or- let the host render to a buffer that will be
919     //     copied back to guest at some sync point. None of those methods not
920     //     implemented and pixmaps are not used with OpenGL anyway ...
921     VALIDATE_CONFIG(config, EGL_FALSE);
922     (void)dpy;
923     (void)pixmap;
924     (void)attrib_list;
925     return EGL_NO_SURFACE;
926 }
927
928 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
929 {
930     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
931     VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
932
933     egl_surface_t* surface(static_cast<egl_surface_t*>(eglSurface));
934     delete surface;
935
936     return EGL_TRUE;
937 }
938
939 static float s_getNativeDpi() {
940     float nativeDPI = 560.0f;
941     const char* dpiPropName = "qemu.sf.lcd_density";
942     char dpiProp[PROPERTY_VALUE_MAX];
943     if (property_get(dpiPropName, dpiProp, NULL) > 0) {
944         nativeDPI = atof(dpiProp);
945     }
946     return nativeDPI;
947 }
948
949 EGLBoolean eglQuerySurface(EGLDisplay dpy, EGLSurface eglSurface, EGLint attribute, EGLint *value)
950 {
951     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
952     VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
953
954     egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
955
956     // Parameters involved in queries of EGL_(HORIZONTAL|VERTICAL)_RESOLUTION
957     float currWidth, currHeight, scaledResolution, effectiveSurfaceDPI;
958     EGLBoolean ret = EGL_TRUE;
959     switch (attribute) {
960         case EGL_CONFIG_ID:
961             ret = s_display.getConfigAttrib(surface->config, EGL_CONFIG_ID, value);
962             break;
963         case EGL_WIDTH:
964             *value = surface->getWidth();
965             break;
966         case EGL_HEIGHT:
967             *value = surface->getHeight();
968             break;
969         case EGL_TEXTURE_FORMAT:
970             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) {
971                 *value = surface->getTextureFormat();
972             }
973             break;
974         case EGL_TEXTURE_TARGET:
975             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) {
976                 *value = surface->getTextureTarget();
977             }
978             break;
979         case EGL_SWAP_BEHAVIOR:
980         {
981             EGLint surfaceType;
982             ret = s_display.getConfigAttrib(surface->config, EGL_SURFACE_TYPE,
983                     &surfaceType);
984             if (ret == EGL_TRUE) {
985                 if (surfaceType & EGL_SWAP_BEHAVIOR_PRESERVED_BIT) {
986                     *value = EGL_BUFFER_PRESERVED;
987                 } else {
988                     *value = EGL_BUFFER_DESTROYED;
989                 }
990             }
991             break;
992         }
993         case EGL_LARGEST_PBUFFER:
994             // not modified for a window or pixmap surface
995             // and we ignore it when creating a PBuffer surface (default is EGL_FALSE)
996             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) *value = EGL_FALSE;
997             break;
998         case EGL_MIPMAP_TEXTURE:
999             // not modified for a window or pixmap surface
1000             // and we ignore it when creating a PBuffer surface (default is 0)
1001             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) *value = false;
1002             break;
1003         case EGL_MIPMAP_LEVEL:
1004             // not modified for a window or pixmap surface
1005             // and we ignore it when creating a PBuffer surface (default is 0)
1006             if (surface->getSurfaceType() & EGL_PBUFFER_BIT) *value = 0;
1007             break;
1008         case EGL_MULTISAMPLE_RESOLVE:
1009             // ignored when creating the surface, return default
1010             *value = EGL_MULTISAMPLE_RESOLVE_DEFAULT;
1011             break;
1012         case EGL_HORIZONTAL_RESOLUTION:
1013             // pixel/mm * EGL_DISPLAY_SCALING
1014             // TODO: get the DPI from avd config
1015             currWidth = surface->getWidth();
1016             scaledResolution = currWidth / surface->getNativeWidth();
1017             effectiveSurfaceDPI =
1018                 scaledResolution * s_getNativeDpi() * EGL_DISPLAY_SCALING;
1019             *value = (EGLint)(effectiveSurfaceDPI);
1020             break;
1021         case EGL_VERTICAL_RESOLUTION:
1022             // pixel/mm * EGL_DISPLAY_SCALING
1023             // TODO: get the real DPI from avd config
1024             currHeight = surface->getHeight();
1025             scaledResolution = currHeight / surface->getNativeHeight();
1026             effectiveSurfaceDPI =
1027                 scaledResolution * s_getNativeDpi() * EGL_DISPLAY_SCALING;
1028             *value = (EGLint)(effectiveSurfaceDPI);
1029             break;
1030         case EGL_PIXEL_ASPECT_RATIO:
1031             // w / h * EGL_DISPLAY_SCALING
1032             // Please don't ask why * EGL_DISPLAY_SCALING, the document says it
1033             *value = 1 * EGL_DISPLAY_SCALING;
1034             break;
1035         case EGL_RENDER_BUFFER:
1036             switch (surface->getSurfaceType()) {
1037                 case EGL_PBUFFER_BIT:
1038                     *value = EGL_BACK_BUFFER;
1039                     break;
1040                 case EGL_PIXMAP_BIT:
1041                     *value = EGL_SINGLE_BUFFER;
1042                     break;
1043                 case EGL_WINDOW_BIT:
1044                     // ignored when creating the surface, return default
1045                     *value = EGL_BACK_BUFFER;
1046                     break;
1047                 default:
1048                     ALOGE("eglQuerySurface %x unknown surface type %x",
1049                             attribute, surface->getSurfaceType());
1050                     ret = setErrorFunc(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1051                     break;
1052             }
1053             break;
1054         case EGL_VG_COLORSPACE:
1055             // ignored when creating the surface, return default
1056             *value = EGL_VG_COLORSPACE_sRGB;
1057             break;
1058         case EGL_VG_ALPHA_FORMAT:
1059             // ignored when creating the surface, return default
1060             *value = EGL_VG_ALPHA_FORMAT_NONPRE;
1061             break;
1062         //TODO: complete other attributes
1063         default:
1064             ALOGE("eglQuerySurface %x  EGL_BAD_ATTRIBUTE", attribute);
1065             ret = setErrorFunc(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1066             break;
1067     }
1068
1069     return ret;
1070 }
1071
1072 EGLBoolean eglBindAPI(EGLenum api)
1073 {
1074     if (api != EGL_OPENGL_ES_API)
1075         setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1076     return EGL_TRUE;
1077 }
1078
1079 EGLenum eglQueryAPI()
1080 {
1081     return EGL_OPENGL_ES_API;
1082 }
1083
1084 EGLBoolean eglWaitClient()
1085 {
1086     return eglWaitGL();
1087 }
1088
1089 EGLBoolean eglReleaseThread()
1090 {
1091     EGLThreadInfo *tInfo = getEGLThreadInfo();
1092     if (tInfo) {
1093         tInfo->eglError = EGL_SUCCESS;
1094         EGLContext_t* context = tInfo->currentContext;
1095         if (context) {
1096             // The following code is doing pretty much the same thing as
1097             // eglMakeCurrent(&s_display, EGL_NO_CONTEXT, EGL_NO_SURFACE, EGL_NO_SURFACE)
1098             // with the only issue that we do not require a valid display here.
1099             DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1100             rcEnc->rcMakeCurrent(rcEnc, 0, 0, 0);
1101              if (context->version == 2) {
1102                 hostCon->gl2Encoder()->setClientState(NULL);
1103                 hostCon->gl2Encoder()->setSharedGroup(GLSharedGroupPtr());
1104             }
1105             else {
1106                 hostCon->glEncoder()->setClientState(NULL);
1107                 hostCon->glEncoder()->setSharedGroup(GLSharedGroupPtr());
1108             }
1109             context->flags &= ~EGLContext_t::IS_CURRENT;
1110
1111             if (context->deletePending) {
1112                 if (context->rcContext) {
1113                     rcEnc->rcDestroyContext(rcEnc, context->rcContext);
1114                     context->rcContext = 0;
1115                 }
1116                 delete context;
1117             }
1118             tInfo->currentContext = 0;
1119         }
1120     }
1121     return EGL_TRUE;
1122 }
1123
1124 EGLSurface eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint *attrib_list)
1125 {
1126     //TODO
1127     (void)dpy;
1128     (void)buftype;
1129     (void)buffer;
1130     (void)config;
1131     (void)attrib_list;
1132     ALOGW("%s not implemented", __FUNCTION__);
1133     return 0;
1134 }
1135
1136 EGLBoolean eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
1137 {
1138     // Right now we don't do anything when using host GPU.
1139     // This is purely just to pass the data through
1140     // without issuing a warning. We may benefit from validating the
1141     // display and surface for debug purposes.
1142     // TODO: Find cases where we actually need to do something.
1143     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1144     VALIDATE_SURFACE_RETURN(surface, EGL_FALSE);
1145     if (surface == EGL_NO_SURFACE) {
1146         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1147     }
1148
1149     (void)value;
1150
1151     egl_surface_t* p_surface( static_cast<egl_surface_t*>(surface) );
1152     switch (attribute) {
1153     case EGL_MIPMAP_LEVEL:
1154         return true;
1155         break;
1156     case EGL_MULTISAMPLE_RESOLVE:
1157     {
1158         if (value == EGL_MULTISAMPLE_RESOLVE_BOX) {
1159             EGLint surface_type;
1160             s_display.getConfigAttrib(p_surface->config, EGL_SURFACE_TYPE, &surface_type);
1161             if (0 == (surface_type & EGL_MULTISAMPLE_RESOLVE_BOX_BIT)) {
1162                 setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1163             }
1164         }
1165         return true;
1166         break;
1167     }
1168     case EGL_SWAP_BEHAVIOR:
1169         if (value == EGL_BUFFER_PRESERVED) {
1170             EGLint surface_type;
1171             s_display.getConfigAttrib(p_surface->config, EGL_SURFACE_TYPE, &surface_type);
1172             if (0 == (surface_type & EGL_SWAP_BEHAVIOR_PRESERVED_BIT)) {
1173                 setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1174             }
1175         }
1176         return true;
1177         break;
1178     default:
1179         ALOGW("%s: attr=0x%x not implemented", __FUNCTION__, attribute);
1180         setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1181     }
1182     return false;
1183 }
1184
1185 EGLBoolean eglBindTexImage(EGLDisplay dpy, EGLSurface eglSurface, EGLint buffer)
1186 {
1187     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1188     VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
1189     if (eglSurface == EGL_NO_SURFACE) {
1190         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1191     }
1192
1193     if (buffer != EGL_BACK_BUFFER) {
1194         setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1195     }
1196
1197     egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
1198
1199     if (surface->getTextureFormat() == EGL_NO_TEXTURE) {
1200         setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1201     }
1202
1203     if (!(surface->getSurfaceType() & EGL_PBUFFER_BIT)) {
1204         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1205     }
1206
1207     //It's now safe to cast to pbuffer surface
1208     egl_pbuffer_surface_t* pbSurface = (egl_pbuffer_surface_t*)surface;
1209
1210     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1211     rcEnc->rcBindTexture(rcEnc, pbSurface->getRcColorBuffer());
1212
1213     return GL_TRUE;
1214 }
1215
1216 EGLBoolean eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1217 {
1218     //TODO
1219     (void)dpy;
1220     (void)surface;
1221     (void)buffer;
1222     ALOGW("%s not implemented", __FUNCTION__);
1223     return 0;
1224 }
1225
1226 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1227 {
1228     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1229     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1230
1231     EGLContext_t* ctx = getEGLThreadInfo()->currentContext;
1232     if (!ctx) {
1233         setErrorReturn(EGL_BAD_CONTEXT, EGL_FALSE);
1234     }
1235     if (!ctx->draw) {
1236         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1237     }
1238     egl_surface_t* draw(static_cast<egl_surface_t*>(ctx->draw));
1239     draw->setSwapInterval(interval);
1240
1241     rcEnc->rcFBSetSwapInterval(rcEnc, interval); //TODO: implement on the host
1242
1243     return EGL_TRUE;
1244 }
1245
1246 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list)
1247 {
1248     VALIDATE_DISPLAY_INIT(dpy, EGL_NO_CONTEXT);
1249     VALIDATE_CONFIG(config, EGL_NO_CONTEXT);
1250
1251     EGLint version = 1; //default
1252     while (attrib_list && attrib_list[0] != EGL_NONE) {
1253         if (attrib_list[0] == EGL_CONTEXT_CLIENT_VERSION) {
1254             version = attrib_list[1];
1255         } else if (attrib_list[0] == EGL_CONTEXT_PRIORITY_LEVEL_IMG) {
1256             // https://www.khronos.org/registry/egl/extensions/IMG/EGL_IMG_context_priority.txt
1257             // It it not yet supported in the emulator.
1258             ALOGW("EGL_CONTEXT_PRIORITY_LEVEL_IMG not supported, ignored");
1259         } else { // Only the attribute EGL_CONTEXT_CLIENT_VERSION may be specified.
1260             setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_CONTEXT);
1261         }
1262         attrib_list+=2;
1263     }
1264
1265     // Currently only support GLES1 and 2
1266     if (version != 1 && version != 2) {
1267         setErrorReturn(EGL_BAD_CONFIG, EGL_NO_CONTEXT);
1268     }
1269
1270     uint32_t rcShareCtx = 0;
1271     EGLContext_t * shareCtx = NULL;
1272     if (share_context) {
1273         shareCtx = static_cast<EGLContext_t*>(share_context);
1274         rcShareCtx = shareCtx->rcContext;
1275         if (shareCtx->dpy != dpy)
1276             setErrorReturn(EGL_BAD_MATCH, EGL_NO_CONTEXT);
1277     }
1278
1279     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_NO_CONTEXT);
1280     // We've created EGL context. Disconnecting
1281     // would be dangerous at this point.
1282     hostCon->setGrallocOnly(false);
1283
1284     uint32_t rcContext = rcEnc->rcCreateContext(rcEnc, (uintptr_t)config, rcShareCtx, version);
1285     if (!rcContext) {
1286         ALOGE("rcCreateContext returned 0");
1287         setErrorReturn(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1288     }
1289
1290     EGLContext_t * context = new EGLContext_t(dpy, config, shareCtx);
1291     if (!context)
1292         setErrorReturn(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1293
1294     context->version = version;
1295     context->rcContext = rcContext;
1296
1297
1298     return context;
1299 }
1300
1301 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1302 {
1303     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1304     VALIDATE_CONTEXT_RETURN(ctx, EGL_FALSE);
1305
1306     EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
1307
1308     if (!context) return EGL_TRUE;
1309
1310     if (getEGLThreadInfo()->currentContext == context) {
1311         getEGLThreadInfo()->currentContext->deletePending = 1;
1312         return EGL_TRUE;
1313     }
1314
1315     if (context->rcContext) {
1316         DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1317         rcEnc->rcDestroyContext(rcEnc, context->rcContext);
1318         context->rcContext = 0;
1319     }
1320
1321     delete context;
1322     return EGL_TRUE;
1323 }
1324
1325 EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
1326 {
1327     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1328     VALIDATE_SURFACE_RETURN(draw, EGL_FALSE);
1329     VALIDATE_SURFACE_RETURN(read, EGL_FALSE);
1330
1331     if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1332         setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1333     if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1334         setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1335
1336     EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
1337     uint32_t ctxHandle = (context) ? context->rcContext : 0;
1338     egl_surface_t * drawSurf = static_cast<egl_surface_t *>(draw);
1339     uint32_t drawHandle = (drawSurf) ? drawSurf->getRcSurface() : 0;
1340     egl_surface_t * readSurf = static_cast<egl_surface_t *>(read);
1341     uint32_t readHandle = (readSurf) ? readSurf->getRcSurface() : 0;
1342
1343     //
1344     // Nothing to do if no binding change has made
1345     //
1346     EGLThreadInfo *tInfo = getEGLThreadInfo();
1347
1348     if (tInfo->currentContext == context &&
1349         (context == NULL ||
1350         (context && context->draw == draw && context->read == read))) {
1351         return EGL_TRUE;
1352     }
1353
1354     if (tInfo->currentContext && tInfo->currentContext->deletePending) {
1355         if (tInfo->currentContext != context) {
1356             EGLContext_t * contextToDelete = tInfo->currentContext;
1357             tInfo->currentContext = 0;
1358             eglDestroyContext(dpy, contextToDelete);
1359         }
1360     }
1361
1362     if (context && (context->flags & EGLContext_t::IS_CURRENT) && (context != tInfo->currentContext)) {
1363         //context is current to another thread
1364         setErrorReturn(EGL_BAD_ACCESS, EGL_FALSE);
1365     }
1366
1367     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1368     if (rcEnc->rcMakeCurrent(rcEnc, ctxHandle, drawHandle, readHandle) == EGL_FALSE) {
1369         ALOGE("rcMakeCurrent returned EGL_FALSE");
1370         setErrorReturn(EGL_BAD_CONTEXT, EGL_FALSE);
1371     }
1372
1373     //Now make the local bind
1374     if (context) {
1375         // This is a nontrivial context.
1376         // The thread cannot be gralloc-only anymore.
1377         hostCon->setGrallocOnly(false);
1378         context->draw = draw;
1379         context->read = read;
1380         context->flags |= EGLContext_t::IS_CURRENT;
1381         //set the client state
1382         if (context->version == 2) {
1383             hostCon->gl2Encoder()->setClientStateMakeCurrent(context->getClientState());
1384             hostCon->gl2Encoder()->setSharedGroup(context->getSharedGroup());
1385         }
1386         else {
1387             hostCon->glEncoder()->setClientState(context->getClientState());
1388             hostCon->glEncoder()->setSharedGroup(context->getSharedGroup());
1389         }
1390     }
1391     else if (tInfo->currentContext) {
1392         //release ClientState & SharedGroup
1393         if (tInfo->currentContext->version == 2) {
1394             hostCon->gl2Encoder()->setClientState(NULL);
1395             hostCon->gl2Encoder()->setSharedGroup(GLSharedGroupPtr(NULL));
1396         }
1397         else {
1398             hostCon->glEncoder()->setClientState(NULL);
1399             hostCon->glEncoder()->setSharedGroup(GLSharedGroupPtr(NULL));
1400         }
1401
1402     }
1403
1404     if (tInfo->currentContext)
1405         tInfo->currentContext->flags &= ~EGLContext_t::IS_CURRENT;
1406
1407     //Now make current
1408     tInfo->currentContext = context;
1409
1410     //Check maybe we need to init the encoder, if it's first eglMakeCurrent
1411     if (tInfo->currentContext) {
1412         if (tInfo->currentContext->version == 2) {
1413             if (!hostCon->gl2Encoder()->isInitialized()) {
1414                 s_display.gles2_iface()->init();
1415                 hostCon->gl2Encoder()->setInitialized();
1416                 ClientAPIExts::initClientFuncs(s_display.gles2_iface(), 1);
1417             }
1418         }
1419         else {
1420             if (!hostCon->glEncoder()->isInitialized()) {
1421                 s_display.gles_iface()->init();
1422                 hostCon->glEncoder()->setInitialized();
1423                 ClientAPIExts::initClientFuncs(s_display.gles_iface(), 0);
1424             }
1425         }
1426     }
1427
1428     return EGL_TRUE;
1429 }
1430
1431 EGLContext eglGetCurrentContext()
1432 {
1433     return getEGLThreadInfo()->currentContext;
1434 }
1435
1436 EGLSurface eglGetCurrentSurface(EGLint readdraw)
1437 {
1438     EGLContext_t * context = getEGLThreadInfo()->currentContext;
1439     if (!context)
1440         return EGL_NO_SURFACE; //not an error
1441
1442     switch (readdraw) {
1443         case EGL_READ:
1444             return context->read;
1445         case EGL_DRAW:
1446             return context->draw;
1447         default:
1448             setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1449     }
1450 }
1451
1452 EGLDisplay eglGetCurrentDisplay()
1453 {
1454     EGLContext_t * context = getEGLThreadInfo()->currentContext;
1455     if (!context)
1456         return EGL_NO_DISPLAY; //not an error
1457
1458     return context->dpy;
1459 }
1460
1461 EGLBoolean eglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value)
1462 {
1463     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1464     VALIDATE_CONTEXT_RETURN(ctx, EGL_FALSE);
1465
1466     EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
1467
1468     EGLBoolean ret = EGL_TRUE;
1469     switch (attribute) {
1470         case EGL_CONFIG_ID:
1471             ret = s_display.getConfigAttrib(context->config, EGL_CONFIG_ID, value);
1472             break;
1473         case EGL_CONTEXT_CLIENT_TYPE:
1474             *value = EGL_OPENGL_ES_API;
1475             break;
1476         case EGL_CONTEXT_CLIENT_VERSION:
1477             *value = context->version;
1478             break;
1479         case EGL_RENDER_BUFFER:
1480             if (!context->draw)
1481                 *value = EGL_NONE;
1482             else
1483                 *value = EGL_BACK_BUFFER; //single buffer not supported
1484             break;
1485         default:
1486             ALOGE("eglQueryContext %x  EGL_BAD_ATTRIBUTE", attribute);
1487             setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1488             break;
1489     }
1490
1491     return ret;
1492 }
1493
1494 EGLBoolean eglWaitGL()
1495 {
1496     EGLThreadInfo *tInfo = getEGLThreadInfo();
1497     if (!tInfo || !tInfo->currentContext) {
1498         return EGL_FALSE;
1499     }
1500
1501     if (tInfo->currentContext->version == 2) {
1502         s_display.gles2_iface()->finish();
1503     }
1504     else {
1505         s_display.gles_iface()->finish();
1506     }
1507
1508     return EGL_TRUE;
1509 }
1510
1511 EGLBoolean eglWaitNative(EGLint engine)
1512 {
1513     (void)engine;
1514     return EGL_TRUE;
1515 }
1516
1517 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface eglSurface)
1518 {
1519     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1520     if (eglSurface == EGL_NO_SURFACE)
1521         setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1522
1523     DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1524
1525     egl_surface_t* d = static_cast<egl_surface_t*>(eglSurface);
1526     if (d->dpy != dpy)
1527         setErrorReturn(EGL_BAD_DISPLAY, EGL_FALSE);
1528
1529     // post the surface
1530     d->swapBuffers();
1531
1532     hostCon->flush();
1533     return EGL_TRUE;
1534 }
1535
1536 EGLBoolean eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1537 {
1538     //TODO :later
1539     (void)dpy;
1540     (void)surface;
1541     (void)target;
1542     return 0;
1543 }
1544
1545 EGLBoolean eglLockSurfaceKHR(EGLDisplay display, EGLSurface surface, const EGLint *attrib_list)
1546 {
1547     //TODO later
1548     (void)display;
1549     (void)surface;
1550     (void)attrib_list;
1551     return 0;
1552 }
1553
1554 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay display, EGLSurface surface)
1555 {
1556     //TODO later
1557     (void)display;
1558     (void)surface;
1559     return 0;
1560 }
1561
1562 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list)
1563 {
1564     (void)attrib_list;
1565
1566     VALIDATE_DISPLAY_INIT(dpy, EGL_NO_IMAGE_KHR);
1567
1568     if (target == EGL_NATIVE_BUFFER_ANDROID) {
1569         if (ctx != EGL_NO_CONTEXT) {
1570             setErrorReturn(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1571         }
1572
1573         android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
1574
1575         if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
1576             setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1577
1578         if (native_buffer->common.version != sizeof(android_native_buffer_t))
1579             setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1580
1581         cb_handle_t *cb = (cb_handle_t *)(native_buffer->handle);
1582
1583         switch (cb->format) {
1584             case HAL_PIXEL_FORMAT_RGBA_8888:
1585             case HAL_PIXEL_FORMAT_RGBX_8888:
1586             case HAL_PIXEL_FORMAT_RGB_888:
1587             case HAL_PIXEL_FORMAT_RGB_565:
1588             case HAL_PIXEL_FORMAT_YV12:
1589             case HAL_PIXEL_FORMAT_BGRA_8888:
1590                 break;
1591             default:
1592                 setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1593         }
1594
1595         native_buffer->common.incRef(&native_buffer->common);
1596
1597         EGLImage_t *image = new EGLImage_t();
1598         image->dpy = dpy;
1599         image->target = target;
1600         image->native_buffer = native_buffer;
1601
1602         return (EGLImageKHR)image;
1603     }
1604     else if (target == EGL_GL_TEXTURE_2D_KHR) {
1605         VALIDATE_CONTEXT_RETURN(ctx, EGL_NO_IMAGE_KHR);
1606
1607         EGLContext_t *context = static_cast<EGLContext_t*>(ctx);
1608         DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_NO_IMAGE_KHR);
1609
1610         uint32_t ctxHandle = (context) ? context->rcContext : 0;
1611         GLuint texture = (GLuint)reinterpret_cast<uintptr_t>(buffer);
1612         uint32_t img = rcEnc->rcCreateClientImage(rcEnc, ctxHandle, target, texture);
1613         EGLImage_t *image = new EGLImage_t();
1614         image->dpy = dpy;
1615         image->target = target;
1616         image->host_egl_image = img;
1617
1618         return (EGLImageKHR)image;
1619     }
1620     
1621     setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1622 }
1623
1624 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1625 {
1626     VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1627     EGLImage_t *image = (EGLImage_t*)img;
1628
1629     if (!image || image->dpy != dpy) {
1630         RETURN_ERROR(EGL_FALSE, EGL_BAD_PARAMETER);
1631     }
1632
1633     if (image->target == EGL_NATIVE_BUFFER_ANDROID) {
1634         android_native_buffer_t* native_buffer = image->native_buffer;
1635
1636         if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
1637             setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1638
1639         if (native_buffer->common.version != sizeof(android_native_buffer_t))
1640             setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1641
1642         native_buffer->common.decRef(&native_buffer->common);
1643         delete image;
1644
1645         return EGL_TRUE;
1646     }
1647     else if (image->target == EGL_GL_TEXTURE_2D_KHR) {
1648         uint32_t host_egl_image = image->host_egl_image;
1649         delete image;
1650         DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1651         return rcEnc->rcDestroyClientImage(rcEnc, host_egl_image);
1652     }
1653
1654     setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1655 }
1656
1657 #define FENCE_SYNC_HANDLE (EGLSyncKHR)0xFE4CE
1658 #define MAX_EGL_SYNC_ATTRIBS 10
1659
1660 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
1661         const EGLint *attrib_list)
1662 {
1663     VALIDATE_DISPLAY(dpy, EGL_NO_SYNC_KHR);
1664     DPRINT("type for eglCreateSyncKHR: 0x%x", type);
1665
1666     DEFINE_HOST_CONNECTION;
1667
1668     if ((type != EGL_SYNC_FENCE_KHR &&
1669          type != EGL_SYNC_NATIVE_FENCE_ANDROID) ||
1670         (type != EGL_SYNC_FENCE_KHR &&
1671          !rcEnc->hasNativeSync())) {
1672         setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
1673     }
1674
1675     EGLThreadInfo *tInfo = getEGLThreadInfo();
1676     if (!tInfo || !tInfo->currentContext) {
1677         setErrorReturn(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1678     }
1679
1680     int num_actual_attribs = 0;
1681
1682     // If attrib_list is not NULL,
1683     // ensure attrib_list contains (key, value) pairs
1684     // followed by a single EGL_NONE.
1685     // Also validate attribs.
1686     int inputFenceFd = -1;
1687     if (attrib_list) {
1688         for (int i = 0; i < MAX_EGL_SYNC_ATTRIBS; i += 2) {
1689             if (attrib_list[i] == EGL_NONE) {
1690                 num_actual_attribs = i;
1691                 break;
1692             }
1693             if (i + 1 == MAX_EGL_SYNC_ATTRIBS) {
1694                 DPRINT("ERROR: attrib list without EGL_NONE");
1695                 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
1696             }
1697         }
1698
1699         // Validate and input attribs
1700         for (int i = 0; i < num_actual_attribs; i += 2) {
1701             if (attrib_list[i] == EGL_SYNC_TYPE_KHR) {
1702                 DPRINT("ERROR: attrib key = EGL_SYNC_TYPE_KHR");
1703             }
1704             if (attrib_list[i] == EGL_SYNC_STATUS_KHR) {
1705                 DPRINT("ERROR: attrib key = EGL_SYNC_STATUS_KHR");
1706             }
1707             if (attrib_list[i] == EGL_SYNC_CONDITION_KHR) {
1708                 DPRINT("ERROR: attrib key = EGL_SYNC_CONDITION_KHR");
1709             }
1710             EGLint attrib_key = attrib_list[i];
1711             EGLint attrib_val = attrib_list[i + 1];
1712             if (attrib_key == EGL_SYNC_NATIVE_FENCE_FD_ANDROID) {
1713                 if (attrib_val != EGL_NO_NATIVE_FENCE_FD_ANDROID) {
1714                     inputFenceFd = attrib_val;
1715                 }
1716             }
1717             DPRINT("attrib: 0x%x : 0x%x", attrib_key, attrib_val);
1718         }
1719     }
1720
1721     uint64_t sync_handle = 0;
1722     int newFenceFd = -1;
1723
1724     if (rcEnc->hasNativeSync()) {
1725         sync_handle =
1726             createNativeSync(type, attrib_list, num_actual_attribs,
1727                              false /* don't destroy when signaled on the host;
1728                                       let the guest clean this up,
1729                                       because the guest called eglCreateSyncKHR. */,
1730                              inputFenceFd,
1731                              &newFenceFd);
1732
1733     } else {
1734         // Just trigger a glFinish if the native sync on host
1735         // is unavailable.
1736         eglWaitClient();
1737     }
1738
1739     EGLSync_t* syncRes = new EGLSync_t(sync_handle);
1740
1741     if (type == EGL_SYNC_NATIVE_FENCE_ANDROID) {
1742         syncRes->type = EGL_SYNC_NATIVE_FENCE_ANDROID;
1743
1744         if (inputFenceFd < 0) {
1745             syncRes->android_native_fence_fd = newFenceFd;
1746         } else {
1747             DPRINT("has input fence fd %d",
1748                     inputFenceFd);
1749             syncRes->android_native_fence_fd = inputFenceFd;
1750         }
1751     } else {
1752         syncRes->type = EGL_SYNC_FENCE_KHR;
1753         syncRes->android_native_fence_fd = -1;
1754         if (!rcEnc->hasNativeSync()) {
1755             syncRes->status = EGL_SIGNALED_KHR;
1756         }
1757     }
1758
1759     return (EGLSyncKHR)syncRes;
1760 }
1761
1762 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR eglsync)
1763 {
1764     (void)dpy;
1765
1766     if (!eglsync) {
1767         DPRINT("WARNING: null sync object")
1768         return EGL_TRUE;
1769     }
1770
1771     EGLSync_t* sync = static_cast<EGLSync_t*>(eglsync);
1772
1773     if (sync && sync->android_native_fence_fd > 0) {
1774         close(sync->android_native_fence_fd);
1775         sync->android_native_fence_fd = -1;
1776     }
1777
1778     if (sync) {
1779         DEFINE_HOST_CONNECTION;
1780         if (rcEnc->hasNativeSync()) {
1781             rcEnc->rcDestroySyncKHR(rcEnc, sync->handle);
1782         }
1783         delete sync;
1784     }
1785
1786     return EGL_TRUE;
1787 }
1788
1789 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR eglsync, EGLint flags,
1790         EGLTimeKHR timeout)
1791 {
1792     (void)dpy;
1793
1794     if (!eglsync) {
1795         DPRINT("WARNING: null sync object");
1796         return EGL_CONDITION_SATISFIED_KHR;
1797     }
1798
1799     EGLSync_t* sync = (EGLSync_t*)eglsync;
1800
1801     DPRINT("sync=0x%lx (handle=0x%lx) flags=0x%x timeout=0x%llx",
1802            sync, sync->handle, flags, timeout);
1803
1804     DEFINE_HOST_CONNECTION;
1805
1806     EGLint retval;
1807     if (rcEnc->hasNativeSync()) {
1808         retval = rcEnc->rcClientWaitSyncKHR
1809             (rcEnc, sync->handle, flags, timeout);
1810     } else {
1811         retval = EGL_CONDITION_SATISFIED_KHR;
1812     }
1813     EGLint res_status;
1814     switch (sync->type) {
1815         case EGL_SYNC_FENCE_KHR:
1816             res_status = EGL_SIGNALED_KHR;
1817             break;
1818         case EGL_SYNC_NATIVE_FENCE_ANDROID:
1819             res_status = EGL_SYNC_NATIVE_FENCE_SIGNALED_ANDROID;
1820             break;
1821         default:
1822             res_status = EGL_SIGNALED_KHR;
1823     }
1824     sync->status = res_status;
1825     return retval;
1826 }
1827
1828 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR eglsync,
1829         EGLint attribute, EGLint *value)
1830 {
1831     (void)dpy;
1832
1833     EGLSync_t* sync = (EGLSync_t*)eglsync;
1834
1835     switch (attribute) {
1836     case EGL_SYNC_TYPE_KHR:
1837         *value = sync->type;
1838         return EGL_TRUE;
1839     case EGL_SYNC_STATUS_KHR:
1840         *value = sync->status;
1841         return EGL_TRUE;
1842     case EGL_SYNC_CONDITION_KHR:
1843         *value = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
1844         return EGL_TRUE;
1845     default:
1846         setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1847     }
1848 }
1849
1850 int eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR eglsync) {
1851     (void)dpy;
1852
1853     DPRINT("call");
1854
1855     EGLSync_t* sync = (EGLSync_t*)eglsync;
1856     if (sync && sync->android_native_fence_fd > 0) {
1857         int res = dup(sync->android_native_fence_fd);
1858         return res;
1859     } else {
1860         return -1;
1861     }
1862 }
1863
1864 // TODO: Implement EGL_KHR_wait_sync
1865 EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR eglsync, EGLint flags) {
1866     (void)dpy;
1867     (void)eglsync;
1868     (void)flags;
1869     return EGL_TRUE;
1870 }