graw: combine graw_init and graw_create_window functions
authorKeith Whitwell <keithw@vmware.com>
Fri, 14 May 2010 11:12:54 +0000 (12:12 +0100)
committerKeith Whitwell <keithw@vmware.com>
Fri, 14 May 2010 11:19:57 +0000 (12:19 +0100)
Different environments seem to want to create these in different
orders.  Abstract over this by combining the calls.

src/gallium/include/state_tracker/graw.h
src/gallium/targets/graw-xlib/graw_xlib.c
src/gallium/tests/graw/clear.c

index 87e7d97543ab9cac94dc18ad3cbfd2dc7ad22f3a..e5b298e03d9ddb47569b8ee44c118ebc2cc2cd26 100644 (file)
 struct pipe_screen;
 struct pipe_context;
 
-PUBLIC struct pipe_screen *graw_init( void );
-
 /* Returns a handle to be used with flush_frontbuffer()/present().
  *
  * Query format support with screen::is_format_supported and usage
  * XXX.
  */
-PUBLIC void *graw_create_window( int x,
-                                 int y,
-                                 unsigned width,
-                                 unsigned height,
-                                 enum pipe_format format );
+PUBLIC struct pipe_screen *graw_create_window_and_screen( int x,
+                                                          int y,
+                                                          unsigned width,
+                                                          unsigned height,
+                                                          enum pipe_format format,
+                                                          void **handle);
 
-PUBLIC void graw_destroy_window( void *handle );
 PUBLIC void graw_set_display_func( void (*func)( void ) );
 PUBLIC void graw_main_loop( void );
 
index c9a850136a8d72f0650665ab840ae398ed3653ac..41120ba3c70f1b78ffba2eee73f9064fee811b69 100644 (file)
@@ -32,18 +32,14 @@ static struct {
 } 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:
     */
@@ -80,14 +76,16 @@ graw_init( void )
  
 
 
-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;
@@ -96,6 +94,9 @@ 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 );
@@ -107,8 +108,8 @@ graw_create_window( int x,
    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;
 
 
@@ -150,7 +151,6 @@ graw_create_window( int x,
                               None, (char **)NULL, 0, &sizehints);
    }
 
-   XFree(visinfo);
    XMapWindow(graw.display, win);
    while (1) {
       XEvent e;
@@ -160,14 +160,27 @@ 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;
 
-fail:
-   FREE(handle);
    XFree(visinfo);
+   return screen;
+
+fail:
+   if (screen)
+      screen->destroy(screen);
+
+   if (xlib_handle)
+      FREE(xlib_handle);
+
+   if (visinfo)
+      XFree(visinfo);
 
    if (win)
       XDestroyWindow(graw.display, win);
@@ -176,11 +189,6 @@ fail:
 }
 
 
-void
-graw_destroy_window( void *xlib_drawable )
-{
-}
-
 void 
 graw_set_display_func( void (*draw)( void ) )
 {
index adcbb08308fd2b215c307efa182a96d8f2e3f09d..28c986eee64df5c0e5307a2d5257c57cea08673d 100644 (file)
@@ -53,13 +53,20 @@ static void init( void )
    struct pipe_resource *tex, templat;
    int i;
 
+   /* It's hard to say whether window or screen should be created
+    * first.  Different environments would prefer one or the other.
+    *
+    * Also, no easy way of querying supported formats if the screen
+    * cannot be created first.
+    */
    for (i = 0; 
         window == NULL && formats[i] != PIPE_FORMAT_NONE;
         i++) {
       
-      window = graw_create_window(0,0,300,300, formats[i]);
+      screen = graw_create_window_and_screen(0,0,300,300,
+                                             formats[i],
+                                             &window);
    }
-   
    if (window == NULL)
       exit(2);
    
@@ -98,15 +105,11 @@ static void init( void )
 }
 
 
+
 int main( int argc, char *argv[] )
 {
-   screen = graw_init();
-   if (screen == NULL)
-      exit(1);
-
    init();
 
-
    graw_set_display_func( draw );
    graw_main_loop();
    return 0;