gallium: fix refcount bug introduced in eb20e2984
[mesa.git] / src / mesa / drivers / x11 / xm_surface.c
index 6b52b33bf2b0f0b8d35ac1063a82439e8d0d72c5..a3f2fe7d6856a75832aec932511aee940a2fe8c2 100644 (file)
@@ -27,7 +27,7 @@
  * \file xm_surface.c
  * Code to allow the softpipe code to write to X windows/buffers.
  * This is a bit of a hack for now.  We've basically got two different
- * abstractions for color buffers: gl_renderbuffer and softpipe_surface.
+ * abstractions for color buffers: gl_renderbuffer and pipe_surface.
  * They'll need to get merged someday...
  * For now, they're separate things that point to each other.
  */
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
-#include "pipe/softpipe/sp_context.h"
-#include "pipe/softpipe/sp_surface.h"
+#include "pipe/p_winsys.h"
+#include "softpipe/sp_context.h"
+#include "softpipe/sp_clear.h"
+#include "softpipe/sp_tile_cache.h"
+#include "softpipe/sp_surface.h"
 #include "state_tracker/st_context.h"
 
 
-static INLINE struct xmesa_renderbuffer *
-xmesa_rb(struct softpipe_surface *sps)
-{
-   return (struct xmesa_renderbuffer *) sps->surface.rb;
-}
-
+#define CLIP_TILE \
+   do { \
+      if (x + w > ps->width) \
+         w = ps->width - x; \
+      if (y + h > ps->height) \
+         h = ps->height -y; \
+   } while(0)
 
-/**
- * quad reading/writing
- * These functions are just wrappers around the existing renderbuffer
- * functions.
- */
 
-static void
-read_quad_f(struct softpipe_surface *sps, GLint x, GLint y,
-            GLfloat (*rgba)[NUM_CHANNELS])
+static INLINE struct xmesa_surface *
+xmesa_surface(struct pipe_surface *ps)
 {
-   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
-   GLubyte temp[16];
-   GLfloat *dst = (GLfloat *) rgba;
-   GLuint i;
-   GET_CURRENT_CONTEXT(ctx);
-   xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y,     temp);
-   xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y + 1, temp + 8);
-   for (i = 0; i < 16; i++) {
-      dst[i] = UBYTE_TO_FLOAT(temp[i]);
-   }
+   return (struct xmesa_surface *) ps;
 }
 
-static void
-read_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
-                GLfloat (*rrrr)[QUAD_SIZE])
-{
-   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
-   GLubyte temp[16];
-   GLfloat *dst = (GLfloat *) rrrr;
-   GLuint i, j;
-   GET_CURRENT_CONTEXT(ctx);
-   xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y,     temp);
-   xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y + 1, temp + 8);
-   for (i = 0; i < 4; i++) {
-      for (j = 0; j < 4; j++) {
-         dst[j * 4 + i] = UBYTE_TO_FLOAT(temp[i * 4 + j]);
-      }
-   }
-}
 
-static void
-write_quad_f(struct softpipe_surface *sps, GLint x, GLint y,
-             GLfloat (*rgba)[NUM_CHANNELS])
+static INLINE struct xmesa_renderbuffer *
+xmesa_rb(struct pipe_surface *ps)
 {
-   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
-   GLubyte temp[16];
-   const GLfloat *src = (const GLfloat *) rgba;
-   GLuint i;
-   GET_CURRENT_CONTEXT(ctx);
-   for (i = 0; i < 16; i++) {
-      UNCLAMPED_FLOAT_TO_UBYTE(temp[i], src[i]);
-   }
-   xrb->Base.PutRow(ctx, &xrb->Base, 2, x, y,     temp,     NULL);
-   xrb->Base.PutRow(ctx, &xrb->Base, 2, x, y + 1, temp + 8, NULL);
+   struct xmesa_surface *xms = xmesa_surface(ps);
+   return xms->xrb;
 }
 
-static void
-write_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
-                 GLfloat (*rrrr)[QUAD_SIZE])
+
+#define FLIP(Y) Y = xrb->St.Base.Height - (Y) - 1;
+
+
+void
+xmesa_get_tile_rgba(struct pipe_context *pipe, struct pipe_surface *ps,
+                    uint x, uint y, uint w, uint h, float *p)
 {
-   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
-   GLubyte temp[16];
-   const GLfloat *src = (const GLfloat *) rrrr;
-   GLuint i, j;
-   GET_CURRENT_CONTEXT(ctx);
-   for (i = 0; i < 4; i++) {
-      for (j = 0; j < 4; j++) {
-         UNCLAMPED_FLOAT_TO_UBYTE(temp[j * 4 + i], src[i * 4 + j]);
+   struct xmesa_surface *xms = xmesa_surface(ps);
+   struct xmesa_renderbuffer *xrb = xms->xrb;
+
+   if (xrb) {
+      /* this is a front/back color buffer */
+      GLubyte tmp[MAX_WIDTH * 4];
+      GLuint i, j;
+      uint w0 = w;
+      GET_CURRENT_CONTEXT(ctx);
+
+      CLIP_TILE;
+
+      FLIP(y);
+      for (i = 0; i < h; i++) {
+         xrb->St.Base.GetRow(ctx, &xrb->St.Base, w, x, y - i, tmp);
+         for (j = 0; j < w * 4; j++) {
+            p[j] = UBYTE_TO_FLOAT(tmp[j]);
+         }
+         p += w0 * 4;
       }
    }
-   xrb->Base.PutRow(ctx, &xrb->Base, 2, x, y,     temp,     NULL);
-   xrb->Base.PutRow(ctx, &xrb->Base, 2, x, y + 1, temp + 8, NULL);
+   else {
+      /* other softpipe surface */
+      softpipe_get_tile_rgba(ps, x, y, w, h, p);
+   }
 }
 
-static void
-read_quad_ub(struct softpipe_surface *sps, GLint x, GLint y,
-             GLubyte (*rgba)[NUM_CHANNELS])
-{
-   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
-   GET_CURRENT_CONTEXT(ctx);
-   xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y,     rgba);
-   xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y + 1, rgba + 2);
-}
 
-static void
-write_quad_ub(struct softpipe_surface *sps, GLint x, GLint y,
-              GLubyte (*rgba)[NUM_CHANNELS])
+void
+xmesa_put_tile_rgba(struct pipe_context *pipe, struct pipe_surface *ps,
+                    uint x, uint y, uint w, uint h, const float *p)
 {
-   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
-   GET_CURRENT_CONTEXT(ctx);
-   xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y,     rgba);
-   xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y + 1, rgba + 2);
+   struct xmesa_surface *xms = xmesa_surface(ps);
+   struct xmesa_renderbuffer *xrb = xms->xrb;
+
+   if (xrb) {
+      /* this is a front/back color buffer */
+      GLubyte tmp[MAX_WIDTH * 4];
+      GLuint i, j;
+      uint w0 = w;
+      GET_CURRENT_CONTEXT(ctx);
+      CLIP_TILE;
+      FLIP(y);
+      for (i = 0; i < h; i++) {
+         for (j = 0; j < w * 4; j++) {
+            UNCLAMPED_FLOAT_TO_UBYTE(tmp[j], p[j]);
+         }
+         xrb->St.Base.PutRow(ctx, &xrb->St.Base, w, x, y - i, tmp, NULL);
+         p += w0 * 4;
+      }
+#if 0 /* debug: flush */
+      {
+         XMesaContext xm = XMESA_CONTEXT(ctx);
+         XSync(xm->display, 0);
+      }
+#endif
+   }
+   else {
+      /* other softpipe surface */
+      softpipe_put_tile_rgba(ps, x, y, w, h, p);
+   }
 }
 
-#if 0
-static void
-write_mono_row_ub(struct softpipe_surface *sps, GLuint count, GLint x, GLint y,
-                  GLubyte rgba[NUM_CHANNELS])
-{
-   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
-   GET_CURRENT_CONTEXT(ctx);
-   xrb->Base.PutMonoRow(ctx, &xrb->Base, count, x, y, rgba, NULL);
-}
-#endif
 
 
 /**
@@ -168,32 +154,98 @@ write_mono_row_ub(struct softpipe_surface *sps, GLuint count, GLint x, GLint y,
  * have special/unique quad read/write functions for X.
  */
 struct pipe_surface *
-xmesa_new_surface(GLcontext *ctx, struct xmesa_renderbuffer *xrb)
+xmesa_new_color_surface(struct pipe_context *pipe, GLuint pipeFormat)
 {
-   struct pipe_context *pipe = ctx->st->pipe;
-   struct softpipe_surface *sps;
-
-   sps = CALLOC_STRUCT(softpipe_surface);
-   if (!sps)
-      return NULL;
+   struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
 
-   sps->surface.rb = xrb; /* XXX only needed for quad funcs above */
-   sps->surface.width = xrb->Base.Width;
-   sps->surface.height = xrb->Base.Height;
+   assert(pipeFormat);
 
-   sps->read_quad_f = read_quad_f;
-   sps->read_quad_f_swz = read_quad_f_swz;
-   sps->read_quad_ub = read_quad_ub;
-   sps->write_quad_f = write_quad_f;
-   sps->write_quad_f_swz = write_quad_f_swz;
-   sps->write_quad_ub = write_quad_ub;
+   xms->surface.format = pipeFormat;
+   xms->surface.refcount = 1;
 
    /* Note, the region we allocate doesn't actually have any storage
     * since we're drawing into an XImage or Pixmap.
     * The region's size will get set in the xmesa_alloc_front/back_storage()
     * functions.
     */
-   sps->surface.region = pipe->region_alloc(pipe, 0, 0, 0);
+   if (pipe)
+      xms->surface.region = pipe->winsys->region_alloc(pipe->winsys,
+                                                       1, 0, 0, 0x0);
 
-   return &sps->surface;
+   return &xms->surface;
 }
+
+
+/**
+ * Called via pipe->surface_alloc() to create new surfaces (textures,
+ * renderbuffers, etc.
+ */
+struct pipe_surface *
+xmesa_surface_alloc(struct pipe_context *pipe, GLuint pipeFormat)
+{
+   struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
+
+   assert(pipe);
+   assert(pipeFormat);
+
+   xms->surface.format = pipeFormat;
+   xms->surface.refcount = 1;
+
+   return &xms->surface;
+}
+
+
+boolean
+xmesa_is_format_supported(struct pipe_context *pipe, uint format)
+{
+   switch( format ) {
+   case PIPE_FORMAT_U_A8_R8_G8_B8:
+   case PIPE_FORMAT_S_R16_G16_B16_A16:
+   case PIPE_FORMAT_S8_Z24:
+      return TRUE;
+   };
+   return FALSE;
+}
+
+
+/**
+ * Called via pipe->clear()
+ */
+void
+xmesa_clear(struct pipe_context *pipe, struct pipe_surface *ps, GLuint value)
+{
+   struct xmesa_renderbuffer *xrb = xmesa_rb(ps);
+
+   /* XXX actually, we should just discard any cached tiles from this
+    * surface since we don't want to accidentally re-use them after clearing.
+    */
+   pipe->flush(pipe, 0);
+
+   {
+      struct softpipe_context *sp = softpipe_context(pipe);
+      if (ps == sp_tile_cache_get_surface(sp->cbuf_cache[0])) {
+         float clear[4];
+         clear[0] = 0.2; /* XXX hack */
+         clear[1] = 0.2;
+         clear[2] = 0.2;
+         clear[3] = 0.2;
+         sp_tile_cache_clear(sp->cbuf_cache[0], clear);
+      }
+   }
+
+   if (xrb && xrb->ximage) {
+      /* clearing back color buffer */
+      GET_CURRENT_CONTEXT(ctx);
+      xmesa_clear_buffers(ctx, BUFFER_BIT_BACK_LEFT);
+   }
+   else if (xrb && xrb->pixmap) {
+      /* clearing front color buffer */
+      GET_CURRENT_CONTEXT(ctx);
+      xmesa_clear_buffers(ctx, BUFFER_BIT_FRONT_LEFT);
+   }
+   else {
+      /* clearing other buffer */
+      softpipe_clear(pipe, ps, value);
+   }
+}
+