2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "HostConnection.h"
19 #include "ThreadInfo.h"
20 #include "eglDisplay.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"
32 #include "ProcessPipe.h"
34 #include "GLEncoder.h"
36 #include "GL2Encoder.h"
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
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
54 #define DPRINT(fmt,...) ALOGD("%s: " fmt, __FUNCTION__, ##__VA_ARGS__);
60 static T setErrorFunc(GLint error, T returnValue) {
61 getEGLThreadInfo()->eglError = error;
65 const char * eglStrError(EGLint 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";
87 #define LOG_EGL_ERRORS 1
91 #define setErrorReturn(error, retVal) \
93 ALOGE("tid %d: %s(%d): error 0x%x (%s)", gettid(), __FUNCTION__, __LINE__, error, eglStrError(error)); \
94 return setErrorFunc(error, retVal); \
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; \
102 #else //!LOG_EGL_ERRORS
104 #define setErrorReturn(error, retVal) return setErrorFunc(error, retVal);
106 #define RETURN_ERROR(ret,err) \
107 getEGLThreadInfo()->eglError = err; \
110 #endif //LOG_EGL_ERRORS
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); \
117 #define VALIDATE_DISPLAY(dpy,ret) \
118 if ((dpy) != (EGLDisplay)&s_display) { \
119 RETURN_ERROR(ret, EGL_BAD_DISPLAY); \
122 #define VALIDATE_DISPLAY_INIT(dpy,ret) \
123 VALIDATE_DISPLAY(dpy, ret) \
124 if (!s_display.initialized()) { \
125 RETURN_ERROR(ret, EGL_NOT_INITIALIZED); \
128 #define DEFINE_HOST_CONNECTION \
129 HostConnection *hostCon = HostConnection::get(); \
130 ExtendedRCEncoderContext *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL)
132 #define DEFINE_AND_VALIDATE_HOST_CONNECTION(ret) \
133 HostConnection *hostCon = HostConnection::get(); \
135 ALOGE("egl: Failed to get host connection\n"); \
138 ExtendedRCEncoderContext *rcEnc = hostCon->rcEncoder(); \
140 ALOGE("egl: Failed to get renderControl encoder context\n"); \
144 #define VALIDATE_CONTEXT_RETURN(context,ret) \
146 RETURN_ERROR(ret,EGL_BAD_CONTEXT); \
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); \
156 // The one and only supported display object.
157 static eglDisplay s_display;
159 EGLContext_t::EGLContext_t(EGLDisplay dpy, EGLConfig config, EGLContext_t* shareCtx) :
162 read(EGL_NO_SURFACE),
163 draw(EGL_NO_SURFACE),
168 rendererString(NULL),
169 shaderVersionString(NULL),
170 extensionString(NULL),
176 clientState = new GLClientState();
178 sharedGroup = shareCtx->getSharedGroup();
180 sharedGroup = GLSharedGroupPtr(new GLSharedGroup());
181 assert(dpy == (EGLDisplay)&s_display);
182 s_display.onCreateContext((EGLContext)this);
185 int EGLContext_t::getGoldfishSyncFd() {
186 if (goldfishSyncFd < 0) {
187 goldfishSyncFd = goldfish_sync_open();
189 return goldfishSyncFd;
192 EGLContext_t::~EGLContext_t()
194 if (goldfishSyncFd > 0) {
195 goldfish_sync_close(goldfishSyncFd);
198 assert(dpy == (EGLDisplay)&s_display);
199 s_display.onDestroyContext((EGLContext)this);
201 delete [] versionString;
202 delete [] vendorString;
203 delete [] rendererString;
204 delete [] shaderVersionString;
205 delete [] extensionString;
208 // ----------------------------------------------------------------------------
211 //we don't need to handle depth since it's handled when window created on the host
213 struct egl_surface_t {
219 egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLint surfaceType);
220 virtual ~egl_surface_t();
222 virtual void setSwapInterval(int interval) = 0;
223 virtual EGLBoolean swapBuffers() = 0;
225 EGLint getSwapBehavior() const;
226 uint32_t getRcSurface() { return rcSurface; }
227 EGLint getSurfaceType() { return surfaceType; }
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; }
247 // Width of the actual window being presented (not the EGL texture)
248 // Give it some default values.
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; }
259 uint32_t rcSurface; //handle to surface created via remote control
262 egl_surface_t::egl_surface_t(EGLDisplay dpy, EGLConfig config, EGLint surfaceType)
263 : dpy(dpy), config(config), surfaceType(surfaceType), rcSurface(0)
267 // prevent div by 0 in EGL_(HORIZONTAL|VERTICAL)_RESOLUTION queries.
270 texFormat = EGL_NO_TEXTURE;
271 texTarget = EGL_NO_TEXTURE;
272 assert(dpy == (EGLDisplay)&s_display);
273 s_display.onCreateSurface((EGLSurface)this);
276 EGLint egl_surface_t::getSwapBehavior() const {
277 return EGL_BUFFER_PRESERVED;
280 egl_surface_t::~egl_surface_t()
282 assert(dpy == (EGLDisplay)&s_display);
283 s_display.onDestroySurface((EGLSurface)this);
286 // ----------------------------------------------------------------------------
287 // egl_window_surface_t
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);
294 virtual ~egl_window_surface_t();
296 virtual void setSwapInterval(int interval);
297 virtual EGLBoolean swapBuffers();
300 egl_window_surface_t(
301 EGLDisplay dpy, EGLConfig config, EGLint surfType,
302 ANativeWindow* window);
305 ANativeWindow* nativeWindow;
306 android_native_buffer_t* buffer;
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),
316 // keep a reference on the window
317 nativeWindow->common.incRef(&nativeWindow->common);
321 EGLBoolean egl_window_surface_t::init()
323 if (nativeWindow->dequeueBuffer_DEPRECATED(nativeWindow, &buffer) != NO_ERROR) {
324 setErrorReturn(EGL_BAD_ALLOC, EGL_FALSE);
326 setWidth(buffer->width);
327 setHeight(buffer->height);
329 int nativeWidth, nativeHeight;
330 nativeWindow->query(nativeWindow, NATIVE_WINDOW_WIDTH, &nativeWidth);
331 nativeWindow->query(nativeWindow, NATIVE_WINDOW_HEIGHT, &nativeHeight);
333 setNativeWidth(nativeWidth);
334 setNativeHeight(nativeHeight);
336 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
337 rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uintptr_t)config,
338 getWidth(), getHeight());
340 ALOGE("rcCreateWindowSurface returned 0");
343 rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface,
344 ((cb_handle_t*)(buffer->handle))->hostHandle);
349 egl_window_surface_t* egl_window_surface_t::create(
350 EGLDisplay dpy, EGLConfig config, EGLint surfType,
351 ANativeWindow* window)
353 egl_window_surface_t* wnd = new egl_window_surface_t(
354 dpy, config, surfType, window);
355 if (wnd && !wnd->init()) {
362 egl_window_surface_t::~egl_window_surface_t() {
363 DEFINE_HOST_CONNECTION;
364 if (rcSurface && rcEnc) {
365 rcEnc->rcDestroyWindowSurface(rcEnc, rcSurface);
368 nativeWindow->cancelBuffer_DEPRECATED(nativeWindow, buffer);
370 nativeWindow->common.decRef(&nativeWindow->common);
373 void egl_window_surface_t::setSwapInterval(int interval)
375 nativeWindow->setSwapInterval(nativeWindow, interval);
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,
388 DEFINE_HOST_CONNECTION;
390 uint64_t sync_handle;
391 uint64_t thread_handle;
393 EGLint* actual_attribs =
394 (EGLint*)(num_actual_attribs == 0 ? NULL : attrib_list);
396 rcEnc->rcCreateSyncKHR(rcEnc, type,
398 num_actual_attribs * sizeof(EGLint),
399 destroy_when_signaled,
403 if (type == EGL_SYNC_NATIVE_FENCE_ANDROID && fd_in < 0) {
405 goldfish_sync_queue_work(
406 getEGLThreadInfo()->currentContext->getGoldfishSyncFd(),
411 DPRINT("got native fence fd=%d queue_work_err=%d",
412 *fd_out, queue_work_err);
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 */,
434 EGLBoolean egl_window_surface_t::swapBuffers()
437 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
439 // Follow up flushWindowColorBuffer with a fence command.
440 // When the fence command finishes,
441 // we're sure that the buffer on the host
444 // |presentFenceFd| guards the presentation of the
445 // current frame with a goldfish sync fence fd.
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.
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.
455 int presentFenceFd = -1;
457 #if PLATFORM_SDK_VERSION <= 16
458 rcEnc->rcFlushWindowColorBuffer(rcEnc, rcSurface);
459 // equivalent to glFinish if no native sync
461 nativeWindow->queueBuffer(nativeWindow, buffer);
463 if (rcEnc->hasNativeSync()) {
464 rcEnc->rcFlushWindowColorBufferAsync(rcEnc, rcSurface);
465 createGoldfishOpenGLNativeSync(&presentFenceFd);
467 rcEnc->rcFlushWindowColorBuffer(rcEnc, rcSurface);
468 // equivalent to glFinish if no native sync
472 DPRINT("queueBuffer with fence %d", presentFenceFd);
473 nativeWindow->queueBuffer(nativeWindow, buffer, presentFenceFd);
476 DPRINT("calling dequeueBuffer...");
478 #if PLATFORM_SDK_VERSION <= 16
479 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer)) {
481 setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
484 int acquireFenceFd = -1;
485 if (nativeWindow->dequeueBuffer(nativeWindow, &buffer, &acquireFenceFd)) {
487 setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
490 DPRINT("dequeueBuffer with fence %d", acquireFenceFd);
492 if (acquireFenceFd > 0) {
493 close(acquireFenceFd);
497 rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface,
498 ((cb_handle_t *)(buffer->handle))->hostHandle);
500 setWidth(buffer->width);
501 setHeight(buffer->height);
506 // ----------------------------------------------------------------------------
507 //egl_pbuffer_surface_t
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);
513 virtual ~egl_pbuffer_surface_t();
515 virtual void setSwapInterval(int interval) { (void)interval; }
516 virtual EGLBoolean swapBuffers() { return EGL_TRUE; }
518 uint32_t getRcColorBuffer() { return rcColorBuffer; }
521 egl_pbuffer_surface_t(EGLDisplay dpy, EGLConfig config, EGLint surfType,
522 int32_t w, int32_t h);
523 EGLBoolean init(GLenum format);
525 uint32_t rcColorBuffer;
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),
537 egl_pbuffer_surface_t::~egl_pbuffer_surface_t()
539 DEFINE_HOST_CONNECTION;
541 if (rcColorBuffer) rcEnc->rcCloseColorBuffer(rcEnc, rcColorBuffer);
542 if (rcSurface) rcEnc->rcDestroyWindowSurface(rcEnc, rcSurface);
546 EGLBoolean egl_pbuffer_surface_t::init(GLenum pixelFormat)
548 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
550 rcSurface = rcEnc->rcCreateWindowSurface(rcEnc, (uintptr_t)config,
551 getWidth(), getHeight());
553 ALOGE("rcCreateWindowSurface returned 0");
557 rcColorBuffer = rcEnc->rcCreateColorBuffer(rcEnc, getWidth(), getHeight(),
559 if (!rcColorBuffer) {
560 ALOGE("rcCreateColorBuffer returned 0");
564 rcEnc->rcSetWindowColorBuffer(rcEnc, rcSurface, rcColorBuffer);
569 egl_pbuffer_surface_t* egl_pbuffer_surface_t::create(EGLDisplay dpy,
570 EGLConfig config, EGLint surfType, int32_t w, int32_t h,
573 egl_pbuffer_surface_t* pb = new egl_pbuffer_surface_t(dpy, config, surfType,
575 if (pb && !pb->init(pixelFormat)) {
582 static const char *getGLString(int glEnum)
584 EGLThreadInfo *tInfo = getEGLThreadInfo();
585 if (!tInfo || !tInfo->currentContext) {
589 const char** strPtr = NULL;
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
599 strPtr = &tInfo->currentContext->versionString;
602 strPtr = &tInfo->currentContext->vendorString;
605 strPtr = &tInfo->currentContext->rendererString;
607 case GL_SHADING_LANGUAGE_VERSION:
608 strPtr = &tInfo->currentContext->shaderVersionString;
611 strPtr = &tInfo->currentContext->extensionString;
619 if (*strPtr != NULL) {
621 // string is already cached
627 // first query of that string - need to query host
629 DEFINE_AND_VALIDATE_HOST_CONNECTION(NULL);
630 char *hostStr = NULL;
631 int n = rcEnc->rcGetGLString(rcEnc, glEnum, NULL, 0);
633 hostStr = new char[-n+1];
634 n = rcEnc->rcGetGLString(rcEnc, glEnum, hostStr, -n);
642 // keep the string in the context and return its value
648 // ----------------------------------------------------------------------------
650 static EGLClient_eglInterface s_eglIface = {
651 getThreadInfo: getEGLThreadInfo,
652 getGLString: getGLString
655 #define DBG_FUNC DBG("%s\n", __FUNCTION__)
656 EGLDisplay eglGetDisplay(EGLNativeDisplayType display_id)
659 // we support only EGL_DEFAULT_DISPLAY.
661 if (display_id != EGL_DEFAULT_DISPLAY) {
662 return EGL_NO_DISPLAY;
665 return (EGLDisplay)&s_display;
668 EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
670 VALIDATE_DISPLAY(dpy,EGL_FALSE);
672 if (!s_display.initialize(&s_eglIface)) {
676 *major = s_display.getVersionMajor();
678 *minor = s_display.getVersionMinor();
682 EGLBoolean eglTerminate(EGLDisplay dpy)
684 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
686 s_display.terminate();
692 EGLint error = getEGLThreadInfo()->eglError;
693 getEGLThreadInfo()->eglError = EGL_SUCCESS;
697 __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname)
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;
706 // look in gles client api's extensions table
707 return (__eglMustCastToProperFunctionPointerType)ClientAPIExts::getProcAddress(procname);
709 // Fail - function not found.
713 const char* eglQueryString(EGLDisplay dpy, EGLint name)
715 VALIDATE_DISPLAY_INIT(dpy, NULL);
717 return s_display.queryString(name);
720 EGLBoolean eglGetConfigs(EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config)
722 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
725 RETURN_ERROR(EGL_FALSE,EGL_BAD_PARAMETER);
728 GLint numConfigs = s_display.getNumConfigs();
730 *num_config = numConfigs;
735 for (i = 0 ; i < numConfigs && i < config_size ; i++) {
736 *configs++ = (EGLConfig)(uintptr_t)i;
742 EGLBoolean eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
744 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
747 setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
750 int attribs_size = 0;
752 const EGLint * attrib_p = attrib_list;
753 while (attrib_p[0] != EGL_NONE) {
757 attribs_size++; //for the terminating EGL_NONE
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;
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);
782 if (local_attrib_list) delete [] local_attrib_list;
783 if (*num_config <= 0) {
784 EGLint err = -(*num_config);
787 case EGL_BAD_ATTRIBUTE:
788 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
796 for (i=0;i<(*num_config);i++) {
797 *((uintptr_t*)configs+i) = *((uint32_t*)tempConfigs+i);
804 EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
806 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
807 VALIDATE_CONFIG(config, EGL_FALSE);
809 if (s_display.getConfigAttrib(config, attribute, value))
815 RETURN_ERROR(EGL_FALSE, EGL_BAD_ATTRIBUTE);
819 EGLSurface eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list)
823 VALIDATE_DISPLAY_INIT(dpy, NULL);
824 VALIDATE_CONFIG(config, EGL_FALSE);
826 setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
830 if (s_display.getConfigAttrib(config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE) return EGL_FALSE;
832 if (!(surfaceType & EGL_WINDOW_BIT)) {
833 setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
836 if (static_cast<ANativeWindow*>(win)->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
837 setErrorReturn(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE);
840 egl_surface_t* surface = egl_window_surface_t::create(
841 &s_display, config, EGL_WINDOW_BIT, static_cast<ANativeWindow*>(win));
843 setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
849 EGLSurface eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
851 VALIDATE_DISPLAY_INIT(dpy, NULL);
852 VALIDATE_CONFIG(config, EGL_FALSE);
855 if (s_display.getConfigAttrib(config, EGL_SURFACE_TYPE, &surfaceType) == EGL_FALSE) return EGL_FALSE;
857 if (!(surfaceType & EGL_PBUFFER_BIT)) {
858 setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
863 EGLint texFormat = EGL_NO_TEXTURE;
864 EGLint texTarget = EGL_NO_TEXTURE;
865 while (attrib_list[0] != EGL_NONE) {
866 switch (attrib_list[0]) {
869 if (w < 0) setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
873 if (h < 0) setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
875 case EGL_TEXTURE_FORMAT:
876 texFormat = attrib_list[1];
878 case EGL_TEXTURE_TARGET:
879 texTarget = attrib_list[1];
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:
888 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
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);
896 // TODO: check EGL_TEXTURE_FORMAT - need to support eglBindTexImage
899 if (s_display.getConfigGLPixelFormat(config, &pixelFormat) == EGL_FALSE)
900 setErrorReturn(EGL_BAD_MATCH, EGL_NO_SURFACE);
902 egl_surface_t* surface = egl_pbuffer_surface_t::create(dpy, config,
903 EGL_PBUFFER_BIT, w, h, pixelFormat);
905 setErrorReturn(EGL_BAD_ALLOC, EGL_NO_SURFACE);
909 surface->setTextureFormat(texFormat);
910 surface->setTextureTarget(texTarget);
915 EGLSurface eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config, EGLNativePixmapType pixmap, const EGLint *attrib_list)
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);
926 return EGL_NO_SURFACE;
929 EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
931 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
932 VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
934 egl_surface_t* surface(static_cast<egl_surface_t*>(eglSurface));
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);
950 EGLBoolean eglQuerySurface(EGLDisplay dpy, EGLSurface eglSurface, EGLint attribute, EGLint *value)
952 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
953 VALIDATE_SURFACE_RETURN(eglSurface, EGL_FALSE);
955 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
957 // Parameters involved in queries of EGL_(HORIZONTAL|VERTICAL)_RESOLUTION
958 float currWidth, currHeight, scaledResolution, effectiveSurfaceDPI;
959 EGLBoolean ret = EGL_TRUE;
962 ret = s_display.getConfigAttrib(surface->config, EGL_CONFIG_ID, value);
965 *value = surface->getWidth();
968 *value = surface->getHeight();
970 case EGL_TEXTURE_FORMAT:
971 if (surface->getSurfaceType() & EGL_PBUFFER_BIT) {
972 *value = surface->getTextureFormat();
975 case EGL_TEXTURE_TARGET:
976 if (surface->getSurfaceType() & EGL_PBUFFER_BIT) {
977 *value = surface->getTextureTarget();
980 case EGL_SWAP_BEHAVIOR:
983 ret = s_display.getConfigAttrib(surface->config, EGL_SURFACE_TYPE,
985 if (ret == EGL_TRUE) {
986 if (surfaceType & EGL_SWAP_BEHAVIOR_PRESERVED_BIT) {
987 *value = EGL_BUFFER_PRESERVED;
989 *value = EGL_BUFFER_DESTROYED;
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;
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;
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;
1009 case EGL_MULTISAMPLE_RESOLVE:
1010 // ignored when creating the surface, return default
1011 *value = EGL_MULTISAMPLE_RESOLVE_DEFAULT;
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);
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);
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;
1036 case EGL_RENDER_BUFFER:
1037 switch (surface->getSurfaceType()) {
1038 case EGL_PBUFFER_BIT:
1039 *value = EGL_BACK_BUFFER;
1041 case EGL_PIXMAP_BIT:
1042 *value = EGL_SINGLE_BUFFER;
1044 case EGL_WINDOW_BIT:
1045 // ignored when creating the surface, return default
1046 *value = EGL_BACK_BUFFER;
1049 ALOGE("eglQuerySurface %x unknown surface type %x",
1050 attribute, surface->getSurfaceType());
1051 ret = setErrorFunc(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1055 case EGL_VG_COLORSPACE:
1056 // ignored when creating the surface, return default
1057 *value = EGL_VG_COLORSPACE_sRGB;
1059 case EGL_VG_ALPHA_FORMAT:
1060 // ignored when creating the surface, return default
1061 *value = EGL_VG_ALPHA_FORMAT_NONPRE;
1063 //TODO: complete other attributes
1065 ALOGE("eglQuerySurface %x EGL_BAD_ATTRIBUTE", attribute);
1066 ret = setErrorFunc(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1073 EGLBoolean eglBindAPI(EGLenum api)
1075 if (api != EGL_OPENGL_ES_API)
1076 setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1080 EGLenum eglQueryAPI()
1082 return EGL_OPENGL_ES_API;
1085 EGLBoolean eglWaitClient()
1090 EGLBoolean eglReleaseThread()
1092 EGLThreadInfo *tInfo = getEGLThreadInfo();
1094 tInfo->eglError = EGL_SUCCESS;
1095 EGLContext_t* context = tInfo->currentContext;
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());
1107 hostCon->glEncoder()->setClientState(NULL);
1108 hostCon->glEncoder()->setSharedGroup(GLSharedGroupPtr());
1110 context->flags &= ~EGLContext_t::IS_CURRENT;
1112 if (context->deletePending) {
1113 if (context->rcContext) {
1114 rcEnc->rcDestroyContext(rcEnc, context->rcContext);
1115 context->rcContext = 0;
1119 tInfo->currentContext = 0;
1125 EGLSurface eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, EGLConfig config, const EGLint *attrib_list)
1133 ALOGW("%s not implemented", __FUNCTION__);
1137 EGLBoolean eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value)
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);
1152 egl_surface_t* p_surface( static_cast<egl_surface_t*>(surface) );
1153 switch (attribute) {
1154 case EGL_MIPMAP_LEVEL:
1157 case EGL_MULTISAMPLE_RESOLVE:
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);
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);
1180 ALOGW("%s: attr=0x%x not implemented", __FUNCTION__, attribute);
1181 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1186 EGLBoolean eglBindTexImage(EGLDisplay dpy, EGLSurface eglSurface, EGLint buffer)
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);
1194 if (buffer != EGL_BACK_BUFFER) {
1195 setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1198 egl_surface_t* surface( static_cast<egl_surface_t*>(eglSurface) );
1200 if (surface->getTextureFormat() == EGL_NO_TEXTURE) {
1201 setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1204 if (!(surface->getSurfaceType() & EGL_PBUFFER_BIT)) {
1205 setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1208 //It's now safe to cast to pbuffer surface
1209 egl_pbuffer_surface_t* pbSurface = (egl_pbuffer_surface_t*)surface;
1211 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1212 rcEnc->rcBindTexture(rcEnc, pbSurface->getRcColorBuffer());
1217 EGLBoolean eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
1223 ALOGW("%s not implemented", __FUNCTION__);
1227 EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval)
1229 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1230 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1232 EGLContext_t* ctx = getEGLThreadInfo()->currentContext;
1234 setErrorReturn(EGL_BAD_CONTEXT, EGL_FALSE);
1237 setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1239 egl_surface_t* draw(static_cast<egl_surface_t*>(ctx->draw));
1240 draw->setSwapInterval(interval);
1242 rcEnc->rcFBSetSwapInterval(rcEnc, interval); //TODO: implement on the host
1247 EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list)
1249 VALIDATE_DISPLAY_INIT(dpy, EGL_NO_CONTEXT);
1250 VALIDATE_CONFIG(config, EGL_NO_CONTEXT);
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 if (attrib_list[0] == EGL_CONTEXT_PRIORITY_LEVEL_IMG) {
1257 // https://www.khronos.org/registry/egl/extensions/IMG/EGL_IMG_context_priority.txt
1258 // It it not yet supported in the emulator.
1259 ALOGW("EGL_CONTEXT_PRIORITY_LEVEL_IMG not supported, ignored");
1260 } else { // Only the attribute EGL_CONTEXT_CLIENT_VERSION may be specified.
1261 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_CONTEXT);
1266 // Currently only support GLES1 and 2
1267 if (version != 1 && version != 2) {
1268 setErrorReturn(EGL_BAD_CONFIG, EGL_NO_CONTEXT);
1271 uint32_t rcShareCtx = 0;
1272 EGLContext_t * shareCtx = NULL;
1273 if (share_context) {
1274 shareCtx = static_cast<EGLContext_t*>(share_context);
1275 rcShareCtx = shareCtx->rcContext;
1276 if (shareCtx->dpy != dpy)
1277 setErrorReturn(EGL_BAD_MATCH, EGL_NO_CONTEXT);
1280 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_NO_CONTEXT);
1281 // We've created EGL context. Disconnecting
1282 // would be dangerous at this point.
1283 hostCon->setGrallocOnly(false);
1285 uint32_t rcContext = rcEnc->rcCreateContext(rcEnc, (uintptr_t)config, rcShareCtx, version);
1287 ALOGE("rcCreateContext returned 0");
1288 setErrorReturn(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1291 EGLContext_t * context = new EGLContext_t(dpy, config, shareCtx);
1293 setErrorReturn(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
1295 context->version = version;
1296 context->rcContext = rcContext;
1302 EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
1304 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1305 VALIDATE_CONTEXT_RETURN(ctx, EGL_FALSE);
1307 EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
1309 if (!context) return EGL_TRUE;
1311 if (getEGLThreadInfo()->currentContext == context) {
1312 getEGLThreadInfo()->currentContext->deletePending = 1;
1316 if (context->rcContext) {
1317 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1318 rcEnc->rcDestroyContext(rcEnc, context->rcContext);
1319 context->rcContext = 0;
1326 EGLBoolean eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
1328 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1329 VALIDATE_SURFACE_RETURN(draw, EGL_FALSE);
1330 VALIDATE_SURFACE_RETURN(read, EGL_FALSE);
1332 if ((read == EGL_NO_SURFACE && draw == EGL_NO_SURFACE) && (ctx != EGL_NO_CONTEXT))
1333 setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1334 if ((read != EGL_NO_SURFACE || draw != EGL_NO_SURFACE) && (ctx == EGL_NO_CONTEXT))
1335 setErrorReturn(EGL_BAD_MATCH, EGL_FALSE);
1337 EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
1338 uint32_t ctxHandle = (context) ? context->rcContext : 0;
1339 egl_surface_t * drawSurf = static_cast<egl_surface_t *>(draw);
1340 uint32_t drawHandle = (drawSurf) ? drawSurf->getRcSurface() : 0;
1341 egl_surface_t * readSurf = static_cast<egl_surface_t *>(read);
1342 uint32_t readHandle = (readSurf) ? readSurf->getRcSurface() : 0;
1345 // Nothing to do if no binding change has made
1347 EGLThreadInfo *tInfo = getEGLThreadInfo();
1349 if (tInfo->currentContext == context &&
1351 (context && context->draw == draw && context->read == read))) {
1355 if (tInfo->currentContext && tInfo->currentContext->deletePending) {
1356 if (tInfo->currentContext != context) {
1357 EGLContext_t * contextToDelete = tInfo->currentContext;
1358 tInfo->currentContext = 0;
1359 eglDestroyContext(dpy, contextToDelete);
1363 if (context && (context->flags & EGLContext_t::IS_CURRENT) && (context != tInfo->currentContext)) {
1364 //context is current to another thread
1365 setErrorReturn(EGL_BAD_ACCESS, EGL_FALSE);
1368 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1369 if (rcEnc->rcMakeCurrent(rcEnc, ctxHandle, drawHandle, readHandle) == EGL_FALSE) {
1370 ALOGE("rcMakeCurrent returned EGL_FALSE");
1371 setErrorReturn(EGL_BAD_CONTEXT, EGL_FALSE);
1374 //Now make the local bind
1376 // This is a nontrivial context.
1377 // The thread cannot be gralloc-only anymore.
1378 hostCon->setGrallocOnly(false);
1379 context->draw = draw;
1380 context->read = read;
1381 context->flags |= EGLContext_t::IS_CURRENT;
1382 //set the client state
1383 if (context->version == 2) {
1384 hostCon->gl2Encoder()->setClientStateMakeCurrent(context->getClientState());
1385 hostCon->gl2Encoder()->setSharedGroup(context->getSharedGroup());
1388 hostCon->glEncoder()->setClientState(context->getClientState());
1389 hostCon->glEncoder()->setSharedGroup(context->getSharedGroup());
1392 else if (tInfo->currentContext) {
1393 //release ClientState & SharedGroup
1394 if (tInfo->currentContext->version == 2) {
1395 hostCon->gl2Encoder()->setClientState(NULL);
1396 hostCon->gl2Encoder()->setSharedGroup(GLSharedGroupPtr(NULL));
1399 hostCon->glEncoder()->setClientState(NULL);
1400 hostCon->glEncoder()->setSharedGroup(GLSharedGroupPtr(NULL));
1405 if (tInfo->currentContext)
1406 tInfo->currentContext->flags &= ~EGLContext_t::IS_CURRENT;
1409 tInfo->currentContext = context;
1411 //Check maybe we need to init the encoder, if it's first eglMakeCurrent
1412 if (tInfo->currentContext) {
1413 if (tInfo->currentContext->version == 2) {
1414 if (!hostCon->gl2Encoder()->isInitialized()) {
1415 s_display.gles2_iface()->init();
1416 hostCon->gl2Encoder()->setInitialized();
1417 ClientAPIExts::initClientFuncs(s_display.gles2_iface(), 1);
1421 if (!hostCon->glEncoder()->isInitialized()) {
1422 s_display.gles_iface()->init();
1423 hostCon->glEncoder()->setInitialized();
1424 ClientAPIExts::initClientFuncs(s_display.gles_iface(), 0);
1432 EGLContext eglGetCurrentContext()
1434 return getEGLThreadInfo()->currentContext;
1437 EGLSurface eglGetCurrentSurface(EGLint readdraw)
1439 EGLContext_t * context = getEGLThreadInfo()->currentContext;
1441 return EGL_NO_SURFACE; //not an error
1445 return context->read;
1447 return context->draw;
1449 setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
1453 EGLDisplay eglGetCurrentDisplay()
1455 EGLContext_t * context = getEGLThreadInfo()->currentContext;
1457 return EGL_NO_DISPLAY; //not an error
1459 return context->dpy;
1462 EGLBoolean eglQueryContext(EGLDisplay dpy, EGLContext ctx, EGLint attribute, EGLint *value)
1464 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1465 VALIDATE_CONTEXT_RETURN(ctx, EGL_FALSE);
1467 EGLContext_t * context = static_cast<EGLContext_t*>(ctx);
1469 EGLBoolean ret = EGL_TRUE;
1470 switch (attribute) {
1472 ret = s_display.getConfigAttrib(context->config, EGL_CONFIG_ID, value);
1474 case EGL_CONTEXT_CLIENT_TYPE:
1475 *value = EGL_OPENGL_ES_API;
1477 case EGL_CONTEXT_CLIENT_VERSION:
1478 *value = context->version;
1480 case EGL_RENDER_BUFFER:
1484 *value = EGL_BACK_BUFFER; //single buffer not supported
1487 ALOGE("eglQueryContext %x EGL_BAD_ATTRIBUTE", attribute);
1488 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1495 EGLBoolean eglWaitGL()
1497 EGLThreadInfo *tInfo = getEGLThreadInfo();
1498 if (!tInfo || !tInfo->currentContext) {
1502 if (tInfo->currentContext->version == 2) {
1503 s_display.gles2_iface()->finish();
1506 s_display.gles_iface()->finish();
1512 EGLBoolean eglWaitNative(EGLint engine)
1518 EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface eglSurface)
1520 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1521 if (eglSurface == EGL_NO_SURFACE)
1522 setErrorReturn(EGL_BAD_SURFACE, EGL_FALSE);
1524 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1526 egl_surface_t* d = static_cast<egl_surface_t*>(eglSurface);
1528 setErrorReturn(EGL_BAD_DISPLAY, EGL_FALSE);
1537 EGLBoolean eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
1546 EGLBoolean eglLockSurfaceKHR(EGLDisplay display, EGLSurface surface, const EGLint *attrib_list)
1555 EGLBoolean eglUnlockSurfaceKHR(EGLDisplay display, EGLSurface surface)
1563 EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attrib_list)
1567 VALIDATE_DISPLAY_INIT(dpy, EGL_NO_IMAGE_KHR);
1569 if (target == EGL_NATIVE_BUFFER_ANDROID) {
1570 if (ctx != EGL_NO_CONTEXT) {
1571 setErrorReturn(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
1574 android_native_buffer_t* native_buffer = (android_native_buffer_t*)buffer;
1576 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
1577 setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1579 if (native_buffer->common.version != sizeof(android_native_buffer_t))
1580 setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1582 cb_handle_t *cb = (cb_handle_t *)(native_buffer->handle);
1584 switch (cb->format) {
1585 case HAL_PIXEL_FORMAT_RGBA_8888:
1586 case HAL_PIXEL_FORMAT_RGBX_8888:
1587 case HAL_PIXEL_FORMAT_RGB_888:
1588 case HAL_PIXEL_FORMAT_RGB_565:
1589 case HAL_PIXEL_FORMAT_YV12:
1590 case HAL_PIXEL_FORMAT_BGRA_8888:
1593 setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1596 native_buffer->common.incRef(&native_buffer->common);
1598 EGLImage_t *image = new EGLImage_t();
1600 image->target = target;
1601 image->native_buffer = native_buffer;
1603 return (EGLImageKHR)image;
1605 else if (target == EGL_GL_TEXTURE_2D_KHR) {
1606 VALIDATE_CONTEXT_RETURN(ctx, EGL_NO_IMAGE_KHR);
1608 EGLContext_t *context = static_cast<EGLContext_t*>(ctx);
1609 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_NO_IMAGE_KHR);
1611 uint32_t ctxHandle = (context) ? context->rcContext : 0;
1612 GLuint texture = (GLuint)reinterpret_cast<uintptr_t>(buffer);
1613 uint32_t img = rcEnc->rcCreateClientImage(rcEnc, ctxHandle, target, texture);
1614 EGLImage_t *image = new EGLImage_t();
1616 image->target = target;
1617 image->host_egl_image = img;
1619 return (EGLImageKHR)image;
1622 setErrorReturn(EGL_BAD_PARAMETER, EGL_NO_IMAGE_KHR);
1625 EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img)
1627 VALIDATE_DISPLAY_INIT(dpy, EGL_FALSE);
1628 EGLImage_t *image = (EGLImage_t*)img;
1630 if (!image || image->dpy != dpy) {
1631 RETURN_ERROR(EGL_FALSE, EGL_BAD_PARAMETER);
1634 if (image->target == EGL_NATIVE_BUFFER_ANDROID) {
1635 android_native_buffer_t* native_buffer = image->native_buffer;
1637 if (native_buffer->common.magic != ANDROID_NATIVE_BUFFER_MAGIC)
1638 setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1640 if (native_buffer->common.version != sizeof(android_native_buffer_t))
1641 setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1643 native_buffer->common.decRef(&native_buffer->common);
1648 else if (image->target == EGL_GL_TEXTURE_2D_KHR) {
1649 uint32_t host_egl_image = image->host_egl_image;
1651 DEFINE_AND_VALIDATE_HOST_CONNECTION(EGL_FALSE);
1652 return rcEnc->rcDestroyClientImage(rcEnc, host_egl_image);
1655 setErrorReturn(EGL_BAD_PARAMETER, EGL_FALSE);
1658 #define FENCE_SYNC_HANDLE (EGLSyncKHR)0xFE4CE
1659 #define MAX_EGL_SYNC_ATTRIBS 10
1661 EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type,
1662 const EGLint *attrib_list)
1664 VALIDATE_DISPLAY(dpy, EGL_NO_SYNC_KHR);
1665 DPRINT("type for eglCreateSyncKHR: 0x%x", type);
1667 DEFINE_HOST_CONNECTION;
1669 if ((type != EGL_SYNC_FENCE_KHR &&
1670 type != EGL_SYNC_NATIVE_FENCE_ANDROID) ||
1671 (type != EGL_SYNC_FENCE_KHR &&
1672 !rcEnc->hasNativeSync())) {
1673 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
1676 EGLThreadInfo *tInfo = getEGLThreadInfo();
1677 if (!tInfo || !tInfo->currentContext) {
1678 setErrorReturn(EGL_BAD_MATCH, EGL_NO_SYNC_KHR);
1681 int num_actual_attribs = 0;
1683 // If attrib_list is not NULL,
1684 // ensure attrib_list contains (key, value) pairs
1685 // followed by a single EGL_NONE.
1686 // Also validate attribs.
1687 int inputFenceFd = -1;
1689 for (int i = 0; i < MAX_EGL_SYNC_ATTRIBS; i += 2) {
1690 if (attrib_list[i] == EGL_NONE) {
1691 num_actual_attribs = i;
1694 if (i + 1 == MAX_EGL_SYNC_ATTRIBS) {
1695 DPRINT("ERROR: attrib list without EGL_NONE");
1696 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_NO_SYNC_KHR);
1700 // Validate and input attribs
1701 for (int i = 0; i < num_actual_attribs; i += 2) {
1702 if (attrib_list[i] == EGL_SYNC_TYPE_KHR) {
1703 DPRINT("ERROR: attrib key = EGL_SYNC_TYPE_KHR");
1705 if (attrib_list[i] == EGL_SYNC_STATUS_KHR) {
1706 DPRINT("ERROR: attrib key = EGL_SYNC_STATUS_KHR");
1708 if (attrib_list[i] == EGL_SYNC_CONDITION_KHR) {
1709 DPRINT("ERROR: attrib key = EGL_SYNC_CONDITION_KHR");
1711 EGLint attrib_key = attrib_list[i];
1712 EGLint attrib_val = attrib_list[i + 1];
1713 if (attrib_key == EGL_SYNC_NATIVE_FENCE_FD_ANDROID) {
1714 if (attrib_val != EGL_NO_NATIVE_FENCE_FD_ANDROID) {
1715 inputFenceFd = attrib_val;
1718 DPRINT("attrib: 0x%x : 0x%x", attrib_key, attrib_val);
1722 uint64_t sync_handle = 0;
1723 int newFenceFd = -1;
1725 if (rcEnc->hasNativeSync()) {
1727 createNativeSync(type, attrib_list, num_actual_attribs,
1728 false /* don't destroy when signaled on the host;
1729 let the guest clean this up,
1730 because the guest called eglCreateSyncKHR. */,
1735 // Just trigger a glFinish if the native sync on host
1740 EGLSync_t* syncRes = new EGLSync_t(sync_handle);
1742 if (type == EGL_SYNC_NATIVE_FENCE_ANDROID) {
1743 syncRes->type = EGL_SYNC_NATIVE_FENCE_ANDROID;
1745 if (inputFenceFd < 0) {
1746 syncRes->android_native_fence_fd = newFenceFd;
1748 DPRINT("has input fence fd %d",
1750 syncRes->android_native_fence_fd = inputFenceFd;
1753 syncRes->type = EGL_SYNC_FENCE_KHR;
1754 syncRes->android_native_fence_fd = -1;
1755 if (!rcEnc->hasNativeSync()) {
1756 syncRes->status = EGL_SIGNALED_KHR;
1760 return (EGLSyncKHR)syncRes;
1763 EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR eglsync)
1768 DPRINT("WARNING: null sync object")
1772 EGLSync_t* sync = static_cast<EGLSync_t*>(eglsync);
1774 if (sync && sync->android_native_fence_fd > 0) {
1775 close(sync->android_native_fence_fd);
1776 sync->android_native_fence_fd = -1;
1780 DEFINE_HOST_CONNECTION;
1781 if (rcEnc->hasNativeSync()) {
1782 rcEnc->rcDestroySyncKHR(rcEnc, sync->handle);
1790 EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR eglsync, EGLint flags,
1796 DPRINT("WARNING: null sync object");
1797 return EGL_CONDITION_SATISFIED_KHR;
1800 EGLSync_t* sync = (EGLSync_t*)eglsync;
1802 DPRINT("sync=0x%lx (handle=0x%lx) flags=0x%x timeout=0x%llx",
1803 sync, sync->handle, flags, timeout);
1805 DEFINE_HOST_CONNECTION;
1808 if (rcEnc->hasNativeSync()) {
1809 retval = rcEnc->rcClientWaitSyncKHR
1810 (rcEnc, sync->handle, flags, timeout);
1812 retval = EGL_CONDITION_SATISFIED_KHR;
1815 switch (sync->type) {
1816 case EGL_SYNC_FENCE_KHR:
1817 res_status = EGL_SIGNALED_KHR;
1819 case EGL_SYNC_NATIVE_FENCE_ANDROID:
1820 res_status = EGL_SYNC_NATIVE_FENCE_SIGNALED_ANDROID;
1823 res_status = EGL_SIGNALED_KHR;
1825 sync->status = res_status;
1829 EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR eglsync,
1830 EGLint attribute, EGLint *value)
1834 EGLSync_t* sync = (EGLSync_t*)eglsync;
1836 switch (attribute) {
1837 case EGL_SYNC_TYPE_KHR:
1838 *value = sync->type;
1840 case EGL_SYNC_STATUS_KHR:
1841 *value = sync->status;
1843 case EGL_SYNC_CONDITION_KHR:
1844 *value = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
1847 setErrorReturn(EGL_BAD_ATTRIBUTE, EGL_FALSE);
1851 int eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR eglsync) {
1856 EGLSync_t* sync = (EGLSync_t*)eglsync;
1857 if (sync && sync->android_native_fence_fd > 0) {
1858 int res = dup(sync->android_native_fence_fd);
1865 // TODO: Implement EGL_KHR_wait_sync
1866 EGLint eglWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR eglsync, EGLint flags) {