Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / gallium / winsys / xlib / xm_winsys.c
index 71ba076bffd05832a74e71a6cba603aef838a7fc..acb5ad8f714ea0299310e23003a019b5ce7f5b48 100644 (file)
@@ -42,8 +42,9 @@
 #include "pipe/p_winsys.h"
 #include "pipe/p_format.h"
 #include "pipe/p_context.h"
-#include "pipe/p_util.h"
 #include "pipe/p_inlines.h"
+#include "util/u_math.h"
+#include "util/u_memory.h"
 #include "softpipe/sp_winsys.h"
 
 #ifdef GALLIUM_CELL
 #define TILE_SIZE 32  /* avoid compilation errors */
 #endif
 
+#ifdef GALLIUM_TRACE
+#include "trace/tr_screen.h"
+#include "trace/tr_context.h"
+#endif
+
 #include "xm_winsys_aub.h"
 
 
 /**
+ * Subclass of pipe_buffer for Xlib winsys.
  * Low-level OS/window system memory buffer
  */
 struct xm_buffer
@@ -74,21 +81,10 @@ struct xm_buffer
 #endif
 };
 
-#if defined(USE_XSHM) && !defined(XFree86Server)
-# define XSHM_ENABLED(b) ((b)->shm)
-#else
-# define XSHM_ENABLED(b) 0
-#endif
-
-struct xmesa_surface
-{
-   struct pipe_surface surface;
-
-   int tileSize;
-   boolean no_swap;
-};
-
 
+/**
+ * Subclass of pipe_winsys for Xlib winsys
+ */
 struct xmesa_pipe_winsys
 {
    struct pipe_winsys base;
@@ -97,30 +93,139 @@ struct xmesa_pipe_winsys
 };
 
 
-static void alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
-    unsigned width, unsigned height);
 
 /** Cast wrapper */
-static INLINE struct xmesa_surface *
-xmesa_surface(struct pipe_surface *ps)
+static INLINE struct xm_buffer *
+xm_buffer( struct pipe_buffer *buf )
 {
-//   assert(0);
-   return (struct xmesa_surface *) ps;
+   return (struct xm_buffer *)buf;
 }
 
 
+/**
+ * X Shared Memory Image extension code
+ */
+#if defined(USE_XSHM) && !defined(XFree86Server)
+
+#define XSHM_ENABLED(b) ((b)->shm)
+
+static volatile int mesaXErrorFlag = 0;
 
 /**
- * Turn the softpipe opaque buffer pointer into a dri_bufmgr opaque
- * buffer pointer...
+ * Catches potential Xlib errors.
  */
-static INLINE struct xm_buffer *
-xm_buffer( struct pipe_buffer *buf )
+static int
+mesaHandleXError(XMesaDisplay *dpy, XErrorEvent *event)
 {
-   return (struct xm_buffer *)buf;
+   (void) dpy;
+   (void) event;
+   mesaXErrorFlag = 1;
+   return 0;
+}
+
+
+static GLboolean alloc_shm(struct xm_buffer *buf, unsigned size)
+{
+   XShmSegmentInfo *const shminfo = & buf->shminfo;
+
+   shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
+   if (shminfo->shmid < 0) {
+      return GL_FALSE;
+   }
+
+   shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
+   if (shminfo->shmaddr == (char *) -1) {
+      shmctl(shminfo->shmid, IPC_RMID, 0);
+      return GL_FALSE;
+   }
+
+   shminfo->readOnly = False;
+   return GL_TRUE;
 }
 
 
+/**
+ * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
+ */
+static void
+alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
+                 unsigned width, unsigned height)
+{
+   /*
+    * We have to do a _lot_ of error checking here to be sure we can
+    * really use the XSHM extension.  It seems different servers trigger
+    * errors at different points if the extension won't work.  Therefore
+    * we have to be very careful...
+    */
+#if 0
+   GC gc;
+#endif
+   int (*old_handler)(XMesaDisplay *, XErrorEvent *);
+
+   b->tempImage = XShmCreateImage(xmb->xm_visual->display,
+                                  xmb->xm_visual->visinfo->visual,
+                                  xmb->xm_visual->visinfo->depth,
+                                  ZPixmap,
+                                  NULL,
+                                  &b->shminfo,
+                                  width, height);
+   if (b->tempImage == NULL) {
+      b->shm = 0;
+      return;
+   }
+
+
+   mesaXErrorFlag = 0;
+   old_handler = XSetErrorHandler(mesaHandleXError);
+   /* This may trigger the X protocol error we're ready to catch: */
+   XShmAttach(xmb->xm_visual->display, &b->shminfo);
+   XSync(xmb->xm_visual->display, False);
+
+   if (mesaXErrorFlag) {
+      /* we are on a remote display, this error is normal, don't print it */
+      XFlush(xmb->xm_visual->display);
+      mesaXErrorFlag = 0;
+      XDestroyImage(b->tempImage);
+      b->tempImage = NULL;
+      b->shm = 0;
+      (void) XSetErrorHandler(old_handler);
+      return;
+   }
+
+
+   /* Finally, try an XShmPutImage to be really sure the extension works */
+#if 0
+   gc = XCreateGC(xmb->xm_visual->display, xmb->drawable, 0, NULL);
+   XShmPutImage(xmb->xm_visual->display, xmb->drawable, gc,
+                b->tempImage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False);
+   XSync(xmb->xm_visual->display, False);
+   XFreeGC(xmb->xm_visual->display, gc);
+   (void) XSetErrorHandler(old_handler);
+   if (mesaXErrorFlag) {
+      XFlush(xmb->xm_visual->display);
+      mesaXErrorFlag = 0;
+      XDestroyImage(b->tempImage);
+      b->tempImage = NULL;
+      b->shm = 0;
+      return;
+   }
+#endif
+}
+
+#else
+
+#define XSHM_ENABLED(b) 0
+
+static void
+alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
+                 unsigned width, unsigned height)
+{
+   b->shm = 0;
+}
+#endif /* USE_XSHM */
+
+
+
 
 /* Most callbacks map direcly onto dri_bufmgr operations:
  */
@@ -170,6 +275,37 @@ xm_buffer_destroy(struct pipe_winsys *pws,
 }
 
 
+/**
+ * For Cell.  Basically, rearrange the pixels/quads from this layout:
+ *  +--+--+--+--+
+ *  |p0|p1|p2|p3|....
+ *  +--+--+--+--+
+ *
+ * to this layout:
+ *  +--+--+
+ *  |p0|p1|....
+ *  +--+--+
+ *  |p2|p3|
+ *  +--+--+
+ */
+static void
+twiddle_tile(const uint *tileIn, uint *tileOut)
+{
+   int y, x;
+
+   for (y = 0; y < TILE_SIZE; y+=2) {
+      for (x = 0; x < TILE_SIZE; x+=2) {
+         int k = 4 * (y/2 * TILE_SIZE/2 + x/2);
+         tileOut[y * TILE_SIZE + (x + 0)] = tileIn[k];
+         tileOut[y * TILE_SIZE + (x + 1)] = tileIn[k+1];
+         tileOut[(y + 1) * TILE_SIZE + (x + 0)] = tileIn[k+2];
+         tileOut[(y + 1) * TILE_SIZE + (x + 1)] = tileIn[k+3];
+      }
+   }
+}
+
+
+
 /**
  * Display a surface that's in a tiled configuration.  That is, all the
  * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
@@ -182,16 +318,16 @@ xmesa_display_surface_tiled(XMesaBuffer b, const struct pipe_surface *surf)
    const uint tilesPerRow = (surf->width + TILE_SIZE - 1) / TILE_SIZE;
    uint x, y;
 
-   /* check that the XImage has been previously initialized */
-   assert(ximage->format);
-   assert(ximage->bitmap_unit);
-
    if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
       alloc_shm_ximage(xm_buf, b, TILE_SIZE, TILE_SIZE);
    }
 
    ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
 
+   /* check that the XImage has been previously initialized */
+   assert(ximage->format);
+   assert(ximage->bitmap_unit);
+
    if (!XSHM_ENABLED(xm_buf)) {
       /* update XImage's fields */
       ximage->width = TILE_SIZE;
@@ -201,24 +337,40 @@ xmesa_display_surface_tiled(XMesaBuffer b, const struct pipe_surface *surf)
 
    for (y = 0; y < surf->height; y += TILE_SIZE) {
       for (x = 0; x < surf->width; x += TILE_SIZE) {
-         int dx = x;
-         int dy = y;
+         uint tmpTile[TILE_SIZE * TILE_SIZE];
          int tx = x / TILE_SIZE;
          int ty = y / TILE_SIZE;
          int offset = ty * tilesPerRow + tx;
-
-         offset *= 4 * TILE_SIZE * TILE_SIZE;
-
-         ximage->data = (char *) xm_buf->data + offset;
-
-         if (XSHM_ENABLED(xm_buf)) {
+         int w = TILE_SIZE;
+         int h = TILE_SIZE;
+
+         if (y + h > surf->height)
+            h = surf->height - y;
+         if (x + w > surf->width)
+            w = surf->width - x;
+
+         /* offset in pixels */
+         offset *= TILE_SIZE * TILE_SIZE;
+
+         if (0 && XSHM_ENABLED(xm_buf)) {
+            ximage->data = (char *) xm_buf->data + 4 * offset;
+            /* make copy of tile data */
+            memcpy(tmpTile, (uint *) ximage->data, sizeof(tmpTile));
+            /* twiddle from temp to ximage in shared memory */
+            twiddle_tile(tmpTile, (uint *) ximage->data);
+            /* display image in shared memory */
 #if defined(USE_XSHM) && !defined(XFree86Server)
             XShmPutImage(b->xm_visual->display, b->drawable, b->gc,
-                         ximage, 0, 0, x, y, TILE_SIZE, TILE_SIZE, False);
+                         ximage, 0, 0, x, y, w, h, False);
 #endif
-         } else {
+         }
+         else {
+            /* twiddle from ximage buffer to temp tile */
+            twiddle_tile((uint *) xm_buf->data + offset, tmpTile);
+            /* display temp tile data */
+            ximage->data = (char *) tmpTile;
             XPutImage(b->xm_visual->display, b->drawable, b->gc,
-                      ximage, 0, 0, dx, dy, TILE_SIZE, TILE_SIZE);
+                      ximage, 0, 0, x, y, w, h);
          }
       }
    }
@@ -234,20 +386,32 @@ xmesa_display_surface(XMesaBuffer b, const struct pipe_surface *surf)
 {
    XImage *ximage;
    struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
-   const struct xmesa_surface *xm_surf
-      = xmesa_surface((struct pipe_surface *) surf);
+   static boolean no_swap = 0;
+   static boolean firsttime = 1;
+   static int tileSize = 0;
 
-   if (xm_surf->no_swap)
+   if (firsttime) {
+      no_swap = getenv("SP_NO_RAST") != NULL;
+#ifdef GALLIUM_CELL
+      if (!getenv("GALLIUM_NOCELL")) {
+         tileSize = 32; /** probably temporary */
+      }
+#endif
+      firsttime = 0;
+   }
+
+   if (no_swap)
       return;
 
-   if (xm_surf->tileSize) {
+   if (tileSize) {
       xmesa_display_surface_tiled(b, surf);
       return;
    }
 
-
    if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
-      alloc_shm_ximage(xm_buf, b, surf->pitch, surf->height);
+      assert(surf->block.width == 1);
+      assert(surf->block.height == 1);
+      alloc_shm_ximage(xm_buf, b, surf->stride/surf->block.size, surf->height);
    }
 
    ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
@@ -267,7 +431,7 @@ xmesa_display_surface(XMesaBuffer b, const struct pipe_surface *surf)
       /* update XImage's fields */
       ximage->width = surf->width;
       ximage->height = surf->height;
-      ximage->bytes_per_line = surf->pitch * surf->cpp;
+      ximage->bytes_per_line = surf->stride;
 
       XPutImage(b->xm_visual->display, b->drawable, b->gc,
                 ximage, 0, 0, 0, 0, surf->width, surf->height);
@@ -297,119 +461,6 @@ xm_get_name(struct pipe_winsys *pws)
 }
 
 
-#if defined(USE_XSHM) && !defined(XFree86Server)
-static volatile int mesaXErrorFlag = 0;
-
-/**
- * Catches potential Xlib errors.
- */
-static int
-mesaHandleXError(XMesaDisplay *dpy, XErrorEvent *event)
-{
-   (void) dpy;
-   (void) event;
-   mesaXErrorFlag = 1;
-   return 0;
-}
-
-
-static GLboolean alloc_shm(struct xm_buffer *buf, unsigned size)
-{
-   XShmSegmentInfo *const shminfo = & buf->shminfo;
-
-   shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
-   if (shminfo->shmid < 0) {
-      return GL_FALSE;
-   }
-
-   shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
-   if (shminfo->shmaddr == (char *) -1) {
-      shmctl(shminfo->shmid, IPC_RMID, 0);
-      return GL_FALSE;
-   }
-
-   shminfo->readOnly = False;
-   return GL_TRUE;
-}
-
-
-/**
- * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
- */
-static void
-alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
-                 unsigned width, unsigned height)
-{
-   /*
-    * We have to do a _lot_ of error checking here to be sure we can
-    * really use the XSHM extension.  It seems different servers trigger
-    * errors at different points if the extension won't work.  Therefore
-    * we have to be very careful...
-    */
-#if 0
-   GC gc;
-#endif
-   int (*old_handler)(XMesaDisplay *, XErrorEvent *);
-
-   b->tempImage = XShmCreateImage(xmb->xm_visual->display,
-                                  xmb->xm_visual->visinfo->visual,
-                                  xmb->xm_visual->visinfo->depth,
-                                  ZPixmap,
-                                  NULL,
-                                  &b->shminfo,
-                                  width, height);
-   if (b->tempImage == NULL) {
-      b->shm = 0;
-      return;
-   }
-
-
-   mesaXErrorFlag = 0;
-   old_handler = XSetErrorHandler(mesaHandleXError);
-   /* This may trigger the X protocol error we're ready to catch: */
-   XShmAttach(xmb->xm_visual->display, &b->shminfo);
-   XSync(xmb->xm_visual->display, False);
-
-   if (mesaXErrorFlag) {
-      /* we are on a remote display, this error is normal, don't print it */
-      XFlush(xmb->xm_visual->display);
-      mesaXErrorFlag = 0;
-      XDestroyImage(b->tempImage);
-      b->tempImage = NULL;
-      b->shm = 0;
-      (void) XSetErrorHandler(old_handler);
-      return;
-   }
-
-
-   /* Finally, try an XShmPutImage to be really sure the extension works */
-#if 0
-   gc = XCreateGC(xmb->xm_visual->display, xmb->drawable, 0, NULL);
-   XShmPutImage(xmb->xm_visual->display, xmb->drawable, gc,
-                b->tempImage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False);
-   XSync(xmb->xm_visual->display, False);
-   XFreeGC(xmb->xm_visual->display, gc);
-   (void) XSetErrorHandler(old_handler);
-   if (mesaXErrorFlag) {
-      XFlush(xmb->xm_visual->display);
-      mesaXErrorFlag = 0;
-      XDestroyImage(b->tempImage);
-      b->tempImage = NULL;
-      b->shm = 0;
-      return;
-   }
-#endif
-}
-#else
-static void
-alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
-                 unsigned width, unsigned height)
-{
-   b->shm = 0;
-}
-#endif
-
-
 static struct pipe_buffer *
 xm_buffer_create(struct pipe_winsys *pws, 
                  unsigned alignment, 
@@ -483,24 +534,29 @@ xm_surface_alloc_storage(struct pipe_winsys *winsys,
                          struct pipe_surface *surf,
                          unsigned width, unsigned height,
                          enum pipe_format format, 
-                         unsigned flags)
+                         unsigned flags,
+                         unsigned tex_usage)
 {
    const unsigned alignment = 64;
 
    surf->width = width;
    surf->height = height;
    surf->format = format;
-   surf->cpp = pf_get_size(format);
-   surf->pitch = round_up(width, alignment / surf->cpp);
-
-#ifdef GALLIUM_CELL /* XXX a bit of a hack */
-   height = round_up(height, TILE_SIZE);
-#endif
+   pf_get_block(format, &surf->block);
+   surf->nblocksx = pf_get_nblocksx(&surf->block, width);
+   surf->nblocksy = pf_get_nblocksy(&surf->block, height);
+   surf->stride = round_up(surf->nblocksx * surf->block.size, alignment);
+   surf->usage = flags;
 
    assert(!surf->buffer);
    surf->buffer = winsys->buffer_create(winsys, alignment,
                                         PIPE_BUFFER_USAGE_PIXEL,
-                                        surf->pitch * surf->cpp * height);
+#ifdef GALLIUM_CELL /* XXX a bit of a hack */
+                                        surf->stride * round_up(surf->nblocksy, TILE_SIZE));
+#else
+                                        surf->stride * surf->nblocksy);
+#endif
+
    if(!surf->buffer)
       return -1;
    
@@ -514,29 +570,14 @@ xm_surface_alloc_storage(struct pipe_winsys *winsys,
 static struct pipe_surface *
 xm_surface_alloc(struct pipe_winsys *ws)
 {
-   struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
-   static boolean no_swap = 0;
-   static boolean firsttime = 1;
-
-   if (firsttime) {
-      no_swap = getenv("SP_NO_RAST") != NULL;
-      firsttime = 0;
-   }
+   struct pipe_surface *surface = CALLOC_STRUCT(pipe_surface);
 
    assert(ws);
 
-   xms->surface.refcount = 1;
-   xms->surface.winsys = ws;
+   surface->refcount = 1;
+   surface->winsys = ws;
 
-#ifdef GALLIUM_CELL
-   if (!getenv("GALLIUM_NOCELL")) {
-      xms->tileSize = 32; /** probably temporary */
-   }
-#endif
-   
-   xms->no_swap = no_swap;
-   
-   return &xms->surface;
+   return surface;
 }
 
 
@@ -545,10 +586,11 @@ static void
 xm_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
 {
    struct pipe_surface *surf = *s;
+   assert(!surf->texture);
    surf->refcount--;
    if (surf->refcount == 0) {
       if (surf->buffer)
-       pipe_buffer_reference(winsys, &surf->buffer, NULL);
+       winsys_buffer_reference(winsys, &surf->buffer, NULL);
       free(surf);
    }
    *s = NULL;
@@ -600,7 +642,7 @@ xmesa_get_pipe_winsys_aub(struct xmesa_visual *xm_vis)
 }
 
 
-struct pipe_winsys *
+static struct pipe_winsys *
 xmesa_get_pipe_winsys(struct xmesa_visual *xm_vis)
 {
    static struct xmesa_pipe_winsys *ws = NULL;
@@ -662,6 +704,12 @@ xmesa_create_pipe_context(XMesaContext xmesa, uint pixelformat)
       struct pipe_screen *screen = softpipe_create_screen(pws);
 
       pipe = softpipe_create(screen, pws, NULL);
+
+#ifdef GALLIUM_TRACE
+      screen = trace_screen_create(screen);
+      
+      pipe = trace_context_create(screen, pipe);
+#endif
    }
 
    if (pipe)