Merge branch 'mesa_7_5_branch'
[mesa.git] / src / gallium / winsys / xlib / xlib_softpipe.c
index f53d8e02f4e28f28eee749497692bc1e69c7c972..44b8464518ab7176d4892fcf1ae2eacaf32cbfa4 100644 (file)
 #undef ASSERT
 #undef Elements
 
-#include "pipe/p_winsys.h"
+#include "pipe/internal/p_winsys_screen.h"
 #include "pipe/p_format.h"
 #include "pipe/p_context.h"
 #include "pipe/p_inlines.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
 #include "softpipe/sp_winsys.h"
+#include "softpipe/sp_texture.h"
 
-#ifdef GALLIUM_CELL
-#include "cell/ppu/cell_context.h"
-#include "cell/ppu/cell_screen.h"
-#include "cell/ppu/cell_winsys.h"
-#else
-#define TILE_SIZE 32  /* avoid compilation errors */
-#endif
-
-#include "xlib_softpipe.h"
+#include "xlib.h"
 
 /**
  * Subclass of pipe_buffer for Xlib winsys.
@@ -66,10 +59,10 @@ struct xm_buffer
    boolean userBuffer;  /** Is this a user-space buffer? */
    void *data;
    void *mapped;
-   
+
    XImage *tempImage;
+#ifdef USE_XSHM
    int shm;
-#if defined(USE_XSHM)
    XShmSegmentInfo shminfo;
 #endif
 };
@@ -82,7 +75,9 @@ struct xmesa_pipe_winsys
 {
    struct pipe_winsys base;
 /*   struct xmesa_visual *xm_visual; */
+#ifdef USE_XSHM
    int shm;
+#endif
 };
 
 
@@ -98,9 +93,13 @@ xm_buffer( struct pipe_buffer *buf )
 /**
  * X Shared Memory Image extension code
  */
-#if defined(USE_XSHM)
-
+#ifdef USE_XSHM
 #define XSHM_ENABLED(b) ((b)->shm)
+#else
+#define XSHM_ENABLED(b) 0
+#endif
+
+#ifdef USE_XSHM
 
 static volatile int mesaXErrorFlag = 0;
 
@@ -150,9 +149,6 @@ alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
     * 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)(Display *, XErrorEvent *);
 
    b->tempImage = XShmCreateImage(xmb->xm_visual->display,
@@ -185,41 +181,13 @@ alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
       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
+   b->shm = 1;
 }
 
-#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:
  */
 static void *
@@ -239,13 +207,12 @@ xm_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buf)
 }
 
 static void
-xm_buffer_destroy(struct pipe_winsys *pws,
-                  struct pipe_buffer *buf)
+xm_buffer_destroy(struct pipe_buffer *buf)
 {
    struct xm_buffer *oldBuf = xm_buffer(buf);
 
    if (oldBuf->data) {
-#if defined(USE_XSHM)
+#ifdef USE_XSHM
       if (oldBuf->shminfo.shmid >= 0) {
          shmdt(oldBuf->shminfo.shmaddr);
          shmctl(oldBuf->shminfo.shmid, IPC_RMID, 0);
@@ -268,156 +235,48 @@ 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.
- */
-void
-xlib_cell_display_surface(struct xmesa_buffer *b, struct pipe_surface *surf)
-{
-   XImage *ximage;
-   struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
-   const uint tilesPerRow = (surf->width + TILE_SIZE - 1) / TILE_SIZE;
-   uint x, y;
-
-   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;
-      ximage->height = TILE_SIZE;
-      ximage->bytes_per_line = TILE_SIZE * 4;
-   }
-
-   for (y = 0; y < surf->height; y += TILE_SIZE) {
-      for (x = 0; x < surf->width; x += TILE_SIZE) {
-         uint tmpTile[TILE_SIZE * TILE_SIZE];
-         int tx = x / TILE_SIZE;
-         int ty = y / TILE_SIZE;
-         int offset = ty * tilesPerRow + tx;
-         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)
-            XShmPutImage(b->xm_visual->display, b->drawable, b->gc,
-                         ximage, 0, 0, x, y, w, h, False);
-#endif
-         }
-         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, x, y, w, h);
-         }
-      }
-   }
-}
-
-
 /**
  * Display/copy the image in the surface into the X window specified
  * by the XMesaBuffer.
  */
-void
-xlib_softpipe_display_surface(struct xmesa_buffer *b, 
+static void
+xlib_softpipe_display_surface(struct xmesa_buffer *b,
                               struct pipe_surface *surf)
 {
    XImage *ximage;
-   struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
+   struct softpipe_texture *spt = softpipe_texture(surf->texture);
+   struct xm_buffer *xm_buf = xm_buffer(spt->buffer);
    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 (no_swap)
       return;
 
-   if (tileSize) {
-      xlib_cell_display_surface(b, surf);
-      return;
-   }
-
+#ifdef USE_XSHM
    if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
-      assert(surf->block.width == 1);
-      assert(surf->block.height == 1);
-      alloc_shm_ximage(xm_buf, b, surf->stride/surf->block.size, surf->height);
+      assert(surf->texture->block.width == 1);
+      assert(surf->texture->block.height == 1);
+      alloc_shm_ximage(xm_buf, b, spt->stride[surf->level] /
+                       surf->texture->block.size, surf->height);
    }
+#endif
 
    ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
    ximage->data = xm_buf->data;
 
    /* display image in Window */
+#ifdef USE_XSHM
    if (XSHM_ENABLED(xm_buf)) {
-#if defined(USE_XSHM)
       XShmPutImage(b->xm_visual->display, b->drawable, b->gc,
                    ximage, 0, 0, 0, 0, surf->width, surf->height, False);
+   } else
 #endif
-   } else {
+   {
       /* check that the XImage has been previously initialized */
       assert(ximage->format);
       assert(ximage->bitmap_unit);
@@ -425,7 +284,7 @@ xlib_softpipe_display_surface(struct xmesa_buffer *b,
       /* update XImage's fields */
       ximage->width = surf->width;
       ximage->height = surf->height;
-      ximage->bytes_per_line = surf->stride;
+      ximage->bytes_per_line = spt->stride[surf->level];
 
       XPutImage(b->xm_visual->display, b->drawable, b->gc,
                 ximage, 0, 0, 0, 0, surf->width, surf->height);
@@ -462,17 +321,9 @@ xm_buffer_create(struct pipe_winsys *pws,
                  unsigned size)
 {
    struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
-#if defined(USE_XSHM)
+#ifdef USE_XSHM
    struct xmesa_pipe_winsys *xpws = (struct xmesa_pipe_winsys *) pws;
-#endif
-
-   buffer->base.refcount = 1;
-   buffer->base.alignment = alignment;
-   buffer->base.usage = usage;
-   buffer->base.size = size;
-
 
-#if defined(USE_XSHM)
    buffer->shminfo.shmid = -1;
    buffer->shminfo.shmaddr = (char *) -1;
 
@@ -481,13 +332,17 @@ xm_buffer_create(struct pipe_winsys *pws,
 
       if (alloc_shm(buffer, size)) {
          buffer->data = buffer->shminfo.shmaddr;
+         buffer->shm = 1;
       }
    }
 #endif
 
-   if (buffer->data == NULL) {
-      buffer->shm = 0;
+   pipe_reference_init(&buffer->base.reference, 1);
+   buffer->base.alignment = alignment;
+   buffer->base.usage = usage;
+   buffer->base.size = size;
 
+   if (buffer->data == NULL) {
       /* align to 16-byte multiple for Cell */
       buffer->data = align_malloc(size, max(alignment, 16));
    }
@@ -503,91 +358,37 @@ static struct pipe_buffer *
 xm_user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
 {
    struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
-   buffer->base.refcount = 1;
+   pipe_reference_init(&buffer->base.reference, 1);
    buffer->base.size = bytes;
    buffer->userBuffer = TRUE;
    buffer->data = ptr;
+#ifdef USE_XSHM
    buffer->shm = 0;
+#endif
 
    return &buffer->base;
 }
 
 
-
-/**
- * Round n up to next multiple.
- */
-static INLINE unsigned
-round_up(unsigned n, unsigned multiple)
-{
-   return (n + multiple - 1) & ~(multiple - 1);
-}
-
-static int
-xm_surface_alloc_storage(struct pipe_winsys *winsys,
-                         struct pipe_surface *surf,
+static struct pipe_buffer *
+xm_surface_buffer_create(struct pipe_winsys *winsys,
                          unsigned width, unsigned height,
-                         enum pipe_format format, 
-                         unsigned flags,
-                         unsigned tex_usage)
+                         enum pipe_format format,
+                         unsigned usage,
+                         unsigned *stride)
 {
    const unsigned alignment = 64;
+   struct pipe_format_block block;
+   unsigned nblocksx, nblocksy;
 
-   surf->width = width;
-   surf->height = height;
-   surf->format = format;
-   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,
-#ifdef GALLIUM_CELL /* XXX a bit of a hack */
-                                        surf->stride * round_up(surf->nblocksy, TILE_SIZE));
-#else
-                                        surf->stride * surf->nblocksy);
-#endif
+   pf_get_block(format, &block);
+   nblocksx = pf_get_nblocksx(&block, width);
+   nblocksy = pf_get_nblocksy(&block, height);
+   *stride = align(nblocksx * block.size, alignment);
 
-   if(!surf->buffer)
-      return -1;
-   
-   return 0;
-}
-
-
-/**
- * Called via winsys->surface_alloc() to create new surfaces.
- */
-static struct pipe_surface *
-xm_surface_alloc(struct pipe_winsys *ws)
-{
-   struct pipe_surface *surface = CALLOC_STRUCT(pipe_surface);
-
-   assert(ws);
-
-   surface->refcount = 1;
-   surface->winsys = ws;
-
-   return surface;
-}
-
-
-
-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)
-       winsys_buffer_reference(winsys, &surf->buffer, NULL);
-      free(surf);
-   }
-   *s = NULL;
+   return winsys->buffer_create(winsys, alignment,
+                                usage,
+                                *stride * nblocksy);
 }
 
 
@@ -620,7 +421,7 @@ xm_fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
 
 
 
-struct pipe_winsys *
+static struct pipe_winsys *
 xlib_create_softpipe_winsys( void )
 {
    static struct xmesa_pipe_winsys *ws = NULL;
@@ -628,8 +429,6 @@ xlib_create_softpipe_winsys( void )
    if (!ws) {
       ws = CALLOC_STRUCT(xmesa_pipe_winsys);
 
-      //ws->shm = xmesa_check_for_xshm( display );
-
       /* Fill in this struct with callbacks that pipe will need to
        * communicate with the window system, buffer manager, etc. 
        */
@@ -639,9 +438,7 @@ xlib_create_softpipe_winsys( void )
       ws->base.buffer_unmap = xm_buffer_unmap;
       ws->base.buffer_destroy = xm_buffer_destroy;
 
-      ws->base.surface_alloc = xm_surface_alloc;
-      ws->base.surface_alloc_storage = xm_surface_alloc_storage;
-      ws->base.surface_release = xm_surface_release;
+      ws->base.surface_buffer_create = xm_surface_buffer_create;
 
       ws->base.fence_reference = xm_fence_reference;
       ws->base.fence_signalled = xm_fence_signalled;
@@ -655,29 +452,37 @@ xlib_create_softpipe_winsys( void )
 }
 
 
-struct pipe_screen *
-xlib_create_softpipe_screen( struct pipe_winsys *pws )
+static struct pipe_screen *
+xlib_create_softpipe_screen( void )
 {
+   struct pipe_winsys *winsys;
    struct pipe_screen *screen;
 
-   screen = softpipe_create_screen(pws);
+   winsys = xlib_create_softpipe_winsys();
+   if (winsys == NULL)
+      return NULL;
+
+   screen = softpipe_create_screen(winsys);
    if (screen == NULL)
       goto fail;
 
    return screen;
 
 fail:
+   if (winsys)
+      winsys->destroy( winsys );
+
    return NULL;
 }
 
 
-struct pipe_context *
+static struct pipe_context *
 xlib_create_softpipe_context( struct pipe_screen *screen,
                               void *context_private )
 {
    struct pipe_context *pipe;
    
-   pipe = softpipe_create(screen, screen->winsys, NULL);
+   pipe = softpipe_create(screen);
    if (pipe == NULL)
       goto fail;
 
@@ -689,55 +494,12 @@ fail:
    return NULL;
 }
 
-
-/***********************************************************************
- * Cell piggybacks on softpipe code still.
- *
- * Should be untangled sufficiently to live in a separate file, at
- * least.  That would mean removing #ifdef GALLIUM_CELL's from above
- * and creating cell-specific versions of either those functions or
- * the entire file.
- */
-struct pipe_winsys *
-xlib_create_cell_winsys( void )
-{
-   return xlib_create_softpipe_winsys();
-}
-
-struct pipe_screen *
-xlib_create_cell_screen( struct pipe_winsys *pws )
+struct xm_driver xlib_softpipe_driver = 
 {
-   return cell_create_screen( pws );
-}
-
-
-struct pipe_context *
-xlib_create_cell_context( struct pipe_screen *screen,
-                          void *priv )
-{
-#ifdef GALLIUM_CELL
-   struct cell_winsys *cws;
-   struct pipe_context *pipe;
-
-   if (getenv("GALLIUM_NOCELL")) 
-      return xlib_create_softpipe_context( screen, priv );
-
-   
-   /* This takes a cell_winsys pointer, but probably that should be
-    * created and stored at screen creation, not context creation.
-    *
-    * The actual cell_winsys value isn't used for anything, so just
-    * passing NULL for now.
-    */
-   pipe = cell_create_context( screen, NULL);
-   if (pipe == NULL)
-      goto fail;
+   .create_pipe_screen = xlib_create_softpipe_screen,
+   .create_pipe_context = xlib_create_softpipe_context,
+   .display_surface = xlib_softpipe_display_surface
+};
 
-   pipe->priv = priv;
 
-   return pipe;
 
-fail:
-#endif
-   return NULL;
-}