gallium/targets/d3dadapter9: drop the libdrm prefix for drm.h
[mesa.git] / src / gallium / targets / graw-xlib / graw_xlib.c
index fb8ef9d78b67b3205f5aa784d846644d36ebec17..db2637bd3c923883068733c75aa244d321e8ff82 100644 (file)
@@ -1,24 +1,13 @@
 #include "pipe/p_compiler.h"
+#include "pipe/p_context.h"
+#include "pipe/p_screen.h"
 #include "util/u_debug.h"
 #include "util/u_memory.h"
-#include "target-helpers/wrap_screen.h"
-#include "state_tracker/xlib_sw_winsys.h"
-
-#ifdef GALLIUM_SOFTPIPE
-#include "softpipe/sp_public.h"
-#endif
-
-#ifdef GALLIUM_LLVMPIPE
-#include "llvmpipe/lp_public.h"
-#endif
-
-/* Haven't figured out a decent way to build the helper code yet -
- * #include it here temporarily.
- */
-#include "sw/sw_public.h"
-#include "sw/sw.c"
-
-#include "graw.h"
+#include "target-helpers/inline_sw_helper.h"
+#include "target-helpers/inline_debug_helper.h"
+#include "state_tracker/xlibsw_api.h"
+#include "state_tracker/graw.h"
+#include "sw/xlib/xlib_sw_winsys.h"
 
 #include <X11/Xlib.h>
 #include <X11/Xlibint.h>
 
 static struct {
    Display *display;
+   void (*draw)(void);
 } graw;
 
 
-struct pipe_screen *
-graw_init( void )
+static struct pipe_screen *
+graw_create_screen( void )
 {
-   const char *default_driver;
-   const char *driver;
    struct pipe_screen *screen = NULL;
    struct sw_winsys *winsys = NULL;
 
-   graw.display = XOpenDisplay(NULL);
-   if (graw.display == NULL)
-      return NULL;
-
    /* Create the underlying winsys, which performs presents to Xlib
     * drawables:
     */
@@ -49,43 +33,24 @@ graw_init( void )
    if (winsys == NULL)
       return NULL;
 
-#if defined(GALLIUM_LLVMPIPE)
-   default_driver = "llvmpipe";
-#elif defined(GALLIUM_SOFTPIPE)
-   default_driver = "softpipe";
-#else
-   default_driver = "";
-#endif
-
-   driver = debug_get_option("GALLIUM_DRIVER", default_driver);
-
-#if defined(GALLIUM_LLVMPIPE)
-   if (screen == NULL && strcmp(driver, "llvmpipe") == 0)
-      screen = llvmpipe_create_screen( winsys );
-#endif
-
-#if defined(GALLIUM_SOFTPIPE)
-   if (screen == NULL)
-      screen = softpipe_create_screen( winsys );
-#endif
+   screen = sw_screen_create( winsys );
 
    /* Inject any wrapping layers we want to here:
     */
-   return gallium_wrap_screen( screen );
+   return debug_screen_wrap( screen );
 }
 
 
-
-
-void *
-graw_create_window( int x,
-                    int y,
-                    unsigned width,
-                    unsigned height,
-                    enum pipe_format format )
+struct pipe_screen *
+graw_create_window_and_screen( int x,
+                               int y,
+                               unsigned width,
+                               unsigned height,
+                               enum pipe_format format,
+                               void **handle)
 {
-   struct xlib_drawable *handle = NULL;
+   struct pipe_screen *screen = NULL;
+   struct xlib_drawable *xlib_handle = NULL;
    XSetWindowAttributes attr;
    Window root;
    Window win = 0;
@@ -94,19 +59,19 @@ graw_create_window( int x,
    int n;
    int scrnum;
 
+   graw.display = XOpenDisplay(NULL);
+   if (graw.display == NULL)
+      return NULL;
 
    scrnum = DefaultScreen( graw.display );
    root = RootWindow( graw.display, scrnum );
 
 
-   if (format != PIPE_FORMAT_R8G8B8A8_UNORM)
-      goto fail;
-
    if (graw.display == NULL)
       goto fail;
 
-   handle = CALLOC_STRUCT(xlib_drawable);
-   if (handle == NULL)
+   xlib_handle = CALLOC_STRUCT(xlib_drawable);
+   if (xlib_handle == NULL)
       goto fail;
 
 
@@ -121,6 +86,23 @@ graw_create_window( int x,
       exit(1);
    }
 
+   /* See if the requirested pixel format matches the visual */
+   if (visinfo->red_mask == 0xff0000 &&
+       visinfo->green_mask == 0xff00 &&
+       visinfo->blue_mask == 0xff) {
+      if (format != PIPE_FORMAT_BGRA8888_UNORM)
+         goto fail;
+   }
+   else if (visinfo->red_mask == 0xff &&
+            visinfo->green_mask == 0xff00 &&
+            visinfo->blue_mask == 0xff0000) {
+      if (format != PIPE_FORMAT_RGBA8888_UNORM)
+         goto fail;
+   }
+   else {
+      goto fail;
+   }
+
    /* window attributes */
    attr.background_pixel = 0;
    attr.border_pixel = 0;
@@ -148,7 +130,6 @@ graw_create_window( int x,
                               None, (char **)NULL, 0, &sizehints);
    }
 
-   XFree(visinfo);
    XMapWindow(graw.display, win);
    while (1) {
       XEvent e;
@@ -158,14 +139,25 @@ graw_create_window( int x,
       }
    }
    
-   handle->visual = visinfo->visual;
-   handle->drawable = (Drawable)win;
-   handle->depth = visinfo->depth;
-   return (void *)handle;
+   xlib_handle->visual = visinfo->visual;
+   xlib_handle->drawable = (Drawable)win;
+   xlib_handle->depth = visinfo->depth;
+   *handle = (void *)xlib_handle;
+
+   screen = graw_create_screen();
+   if (screen == NULL)
+      goto fail;
+
+   free(visinfo);
+   return screen;
 
 fail:
-   FREE(handle);
-   XFree(visinfo);
+   if (screen)
+      screen->destroy(screen);
+
+   FREE(xlib_handle);
+
+   free(visinfo);
 
    if (win)
       XDestroyWindow(graw.display, win);
@@ -174,8 +166,19 @@ fail:
 }
 
 
+void 
+graw_set_display_func( void (*draw)( void ) )
+{
+   graw.draw = draw;
+}
+
 void
-graw_destroy_window( void *xlib_drawable )
+graw_main_loop( void )
 {
+   int i;
+   for (i = 0; i < 10; i++) {
+      graw.draw();
+      sleep(1);
+   }
 }