Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / gallium / winsys / xlib / xm_winsys.c
index b14758f333656ac9fa37ae9d55cb5d01df548e01..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"
 
 
@@ -76,18 +82,6 @@ struct xm_buffer
 };
 
 
-/**
- * Subclass of pipe_surface for Xlib winsys
- */
-struct xmesa_surface
-{
-   struct pipe_surface surface;
-
-   int tileSize;
-   boolean no_swap;
-};
-
-
 /**
  * Subclass of pipe_winsys for Xlib winsys
  */
@@ -100,14 +94,6 @@ struct xmesa_pipe_winsys
 
 
 
-/** Cast wrapper */
-static INLINE struct xmesa_surface *
-xmesa_surface(struct pipe_surface *ps)
-{
-   return (struct xmesa_surface *) ps;
-}
-
-
 /** Cast wrapper */
 static INLINE struct xm_buffer *
 xm_buffer( struct pipe_buffer *buf )
@@ -289,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.
@@ -301,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;
@@ -320,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);
          }
       }
    }
@@ -353,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 (firsttime) {
+      no_swap = getenv("SP_NO_RAST") != NULL;
+#ifdef GALLIUM_CELL
+      if (!getenv("GALLIUM_NOCELL")) {
+         tileSize = 32; /** probably temporary */
+      }
+#endif
+      firsttime = 0;
+   }
 
-   if (xm_surf->no_swap)
+   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;
@@ -386,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);
@@ -497,18 +542,21 @@ xm_surface_alloc_storage(struct pipe_winsys *winsys,
    surf->width = width;
    surf->height = height;
    surf->format = format;
-   surf->cpp = pf_get_size(format);
-   surf->pitch = round_up(width, alignment / surf->cpp);
+   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;
 
-#ifdef GALLIUM_CELL /* XXX a bit of a hack */
-   height = round_up(height, TILE_SIZE);
-#endif
-
    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;
    
@@ -522,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;
 }
 
 
@@ -557,7 +590,7 @@ xm_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
    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;
@@ -671,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)