Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / gallium / winsys / egl_xlib / egl_xlib.c
index 07b3c0eb002de5861e6add5a3e954acbb507f5af..477d766925c8af37a9e191688bb483894bd6dad9 100644 (file)
  */
 
 
+#include <dlfcn.h>
 #include <X11/Xutil.h>
 
 #include "pipe/p_compiler.h"
 #include "pipe/p_format.h"
 #include "pipe/p_state.h"
-#include "pipe/p_util.h"
 #include "pipe/p_winsys.h"
+#include "util/u_memory.h"
 #include "softpipe/sp_winsys.h"
 
 #include "eglconfig.h"
@@ -132,6 +133,10 @@ bitcount(unsigned int n)
 static void
 create_configs(_EGLDriver *drv, EGLDisplay dpy)
 {
+   static const EGLint all_apis = (EGL_OPENGL_ES_BIT |
+                                   EGL_OPENGL_ES2_BIT |
+                                   EGL_OPENVG_BIT |
+                                   EGL_OPENGL_BIT);
    _EGLDisplay *disp = _eglLookupDisplay(dpy);
    XVisualInfo *visInfo, visTemplate;
    int num_visuals, i;
@@ -171,6 +176,10 @@ create_configs(_EGLDriver *drv, EGLDisplay dpy)
       SET_CONFIG_ATTRIB(config, EGL_STENCIL_SIZE, sbits);
       SET_CONFIG_ATTRIB(config, EGL_NATIVE_VISUAL_ID, visid);
       SET_CONFIG_ATTRIB(config, EGL_NATIVE_VISUAL_TYPE, vistype);
+      SET_CONFIG_ATTRIB(config, EGL_NATIVE_RENDERABLE, EGL_FALSE);
+      SET_CONFIG_ATTRIB(config, EGL_CONFORMANT, all_apis);
+      SET_CONFIG_ATTRIB(config, EGL_RENDERABLE_TYPE, all_apis);
+      SET_CONFIG_ATTRIB(config, EGL_SURFACE_TYPE, EGL_WINDOW_BIT);
 
       _eglAddConfig(disp, config);
    }
@@ -202,7 +211,6 @@ xlib_eglInitialize(_EGLDriver *drv, EGLDisplay dpy,
 static EGLBoolean
 xlib_eglTerminate(_EGLDriver *drv, EGLDisplay dpy)
 {
-
    return EGL_TRUE;
 }
 
@@ -210,11 +218,7 @@ xlib_eglTerminate(_EGLDriver *drv, EGLDisplay dpy)
 static _EGLProc
 xlib_eglGetProcAddress(const char *procname)
 {
-   /* XXX for each supported API, evaluate GetProcAddress(name) */
-   /*
-   return _glapi_get_proc_address(procname);
-   */
-   return NULL;
+   return (_EGLProc) st_get_proc_address(procname);
 }
 
 
@@ -294,7 +298,7 @@ display_surface(struct pipe_winsys *pws,
    ximage->data = data;
    ximage->width = psurf->width;
    ximage->height = psurf->height;
-   ximage->bytes_per_line = psurf->pitch * psurf->cpp;
+   ximage->bytes_per_line = psurf->stride;
    
    XPutImage(xsurf->Dpy, xsurf->Win, xsurf->Gc,
              ximage, 0, 0, 0, 0, psurf->width, psurf->height);
@@ -346,6 +350,11 @@ xlib_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
 
    /* API-dependent context creation */
    switch (ctx->Base.ClientAPI) {
+   case EGL_OPENVG_API:
+   case EGL_OPENGL_ES_API:
+      _eglLog(_EGL_DEBUG, "Create Context for ES version %d\n",
+              ctx->Base.ClientVersion);
+      /* fall-through */
    case EGL_OPENGL_API:
       /* create a softpipe context */
       ctx->pipe = softpipe_create(xdrv->screen, xdrv->winsys, NULL);
@@ -376,6 +385,8 @@ xlib_eglDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext ctx)
       else {
          /* API-dependent clean-up */
          switch (context->Base.ClientAPI) {
+         case EGL_OPENGL_ES_API:
+            /* fall-through */
          case EGL_OPENGL_API:
             st_destroy_context(context->Context);
             break;
@@ -411,7 +422,10 @@ xlib_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy,
                    (draw_surf ? draw_surf->Framebuffer : NULL),
                    (read_surf ? read_surf->Framebuffer : NULL));
 
-   check_and_update_buffer_size(draw_surf);
+   if (draw_surf)
+      check_and_update_buffer_size(draw_surf);
+   if (read_surf && read_surf != draw_surf)
+      check_and_update_buffer_size(draw_surf);
 
    return EGL_TRUE;
 }
@@ -522,7 +536,8 @@ xlib_eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
          surf->Base.DeletePending = EGL_TRUE;
       }
       else {
-         st_unreference_framebuffer(&surf->Framebuffer);
+         XFreeGC(surf->Dpy, surf->Gc);
+         st_unreference_framebuffer(surf->Framebuffer);
          free(surf);
       }
       return EGL_TRUE;
@@ -558,6 +573,36 @@ xlib_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
 }
 
 
+/**
+ * Determine which API(s) is(are) present by looking for some specific
+ * global symbols.
+ */
+static EGLint
+find_supported_apis(void)
+{
+   EGLint mask = 0;
+   void *handle;
+
+   handle = dlopen(NULL, 0);
+
+   if (dlsym(handle, "st_api_OpenGL_ES1"))
+      mask |= EGL_OPENGL_ES_BIT;
+
+   if (dlsym(handle, "st_api_OpenGL_ES2"))
+      mask |= EGL_OPENGL_ES2_BIT;
+
+   if (dlsym(handle, "st_api_OpenGL"))
+      mask |= EGL_OPENGL_BIT;
+
+   if (dlsym(handle, "st_api_OpenVG"))
+      mask |= EGL_OPENVG_BIT;
+
+   dlclose(handle);
+
+   return mask;
+}
+
+
 /**
  * This is the main entrypoint into the driver.
  * Called by libEGL to instantiate an _EGLDriver object.
@@ -573,6 +618,10 @@ _eglMain(_EGLDisplay *dpy, const char *args)
    if (!xdrv)
       return NULL;
 
+   if (!dpy->Xdpy) {
+      dpy->Xdpy = XOpenDisplay(NULL);
+   }
+
    _eglInitDriverFallbacks(&xdrv->Base);
    xdrv->Base.API.Initialize = xlib_eglInitialize;
    xdrv->Base.API.Terminate = xlib_eglTerminate;
@@ -584,7 +633,14 @@ _eglMain(_EGLDisplay *dpy, const char *args)
    xdrv->Base.API.MakeCurrent = xlib_eglMakeCurrent;
    xdrv->Base.API.SwapBuffers = xlib_eglSwapBuffers;
 
-   xdrv->Base.ClientAPIsMask = EGL_OPENGL_BIT /*| EGL_OPENGL_ES_BIT*/;
+   xdrv->Base.ClientAPIsMask = find_supported_apis();
+   if (xdrv->Base.ClientAPIsMask == 0x0) {
+      /* the app isn't directly linked with any EGL-supprted APIs
+       * (such as libGLESv2.so) so use an EGL utility to see what
+       * APIs might be loaded dynamically on this system.
+       */
+      xdrv->Base.ClientAPIsMask = _eglFindAPIs();
+   }      
 
    xdrv->Base.Name = "Xlib/softpipe";