Add surface storage allocation function to winsys interface.
authorJosé Fonseca <jrfonseca@tungstengraphics.com>
Tue, 11 Dec 2007 01:14:38 +0000 (01:14 +0000)
committerJosé Fonseca <jrfonseca@tungstengraphics.com>
Tue, 11 Dec 2007 01:14:38 +0000 (01:14 +0000)
src/mesa/drivers/dri/intel_winsys/intel_winsys_pipe.c
src/mesa/pipe/i915simple/i915_surface.c
src/mesa/pipe/p_winsys.h
src/mesa/pipe/softpipe/sp_surface.c
src/mesa/pipe/xlib/xm_winsys.c
src/mesa/state_tracker/st_cb_fbo.c

index 9c643dd0baf4c21e656425011b2e507c1315eb19..c7b519d95be6eb3627387e5254e56dd6d4d7871a 100644 (file)
@@ -178,32 +178,11 @@ intel_flush_frontbuffer( struct pipe_winsys *winsys,
 }
 
 
-static unsigned
-intel_i915_surface_pitch(struct pipe_winsys *winsys,
-                        unsigned cpp, unsigned width, unsigned flags)
-{
-   /* Choose a pitch to match hardware requirements - requires 64 byte
-    * alignment of render targets.  
-    *
-    * XXX: is this ok for textures??
-    * clearly want to be able to render to textures under some
-    * circumstances, but maybe not always a requirement.
-    */
-
-   /* XXX is the pitch different for textures vs. drawables? */
-   if (1/*flags & PIPE_SURFACE_FLAG_TEXTURE*/)  /* or PIPE_SURFACE_FLAG_RENDER? */
-      return ((cpp * width + 63) & ~63) / cpp;
-   else
-      return ((cpp * width + 63) & ~63) / cpp;
-}
-
-
 static struct pipe_surface *
-intel_i915_surface_alloc(struct pipe_winsys *winsys, enum pipe_format format)
+intel_i915_surface_alloc(struct pipe_winsys *winsys)
 {
    struct pipe_surface *surf = CALLOC_STRUCT(pipe_surface);
    if (surf) {
-      surf->format = format;
       surf->refcount = 1;
       surf->winsys = winsys;
    }
@@ -211,6 +190,53 @@ intel_i915_surface_alloc(struct pipe_winsys *winsys, enum pipe_format format)
 }
 
 
+/**
+ * Round n up to next multiple.
+ */
+static INLINE unsigned
+round_up(unsigned n, unsigned multiple)
+{
+   return (n + multiple - 1) & ~(multiple - 1);
+}
+
+/**
+ * Copied from xm_winsys.c
+ */
+static int
+intel_i915_surface_alloc_storage(struct pipe_winsys *winsys,
+                                 struct pipe_surface *surf,
+                                 unsigned width, unsigned height,
+                                 enum pipe_format format, 
+                                 unsigned flags)
+{
+   const unsigned alignment = 64;
+   int ret;
+
+   surf->width = width;
+   surf->height = height;
+   surf->format = format;
+   surf->cpp = pf_get_size(format);
+   surf->pitch = round_up(width, alignment / surf->cpp);
+
+   assert(!surf->buffer);
+   surf->buffer = winsys->buffer_create(winsys, alignment, 0, 0);
+   if(!surf->buffer)
+      return -1;
+
+   ret = winsys->buffer_data(winsys, 
+                             surf->buffer,
+                             surf->pitch * surf->cpp * height,
+                             NULL,
+                             0);
+   if(ret) {
+      winsys->buffer_reference(winsys, &surf->buffer, NULL);
+      return ret;
+   }
+   
+   return 0;
+}
+
+
 static void
 intel_i915_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
 {
@@ -265,8 +291,8 @@ intel_create_pipe_winsys( int fd )
    iws->winsys.flush_frontbuffer = intel_flush_frontbuffer;
    iws->winsys.printf = intel_printf;
    iws->winsys.get_name = intel_get_name;
-   iws->winsys.surface_pitch = intel_i915_surface_pitch;
    iws->winsys.surface_alloc = intel_i915_surface_alloc;
+   iws->winsys.surface_alloc_storage = intel_i915_surface_alloc_storage;
    iws->winsys.surface_release = intel_i915_surface_release;
 
    if (fd)
index bd6fd32704f7ef032a59551b27a00b90af3162be..f93a75b0f0ce98947352a0ceefe8dfb434ff5fff 100644 (file)
@@ -238,11 +238,12 @@ i915_get_tex_surface(struct pipe_context *pipe,
       assert(zslice == 0);
    }
 
-   ps = pipe->winsys->surface_alloc(pipe->winsys, pt->format);
+   ps = pipe->winsys->surface_alloc(pipe->winsys);
    if (ps) {
       assert(ps->format);
       assert(ps->refcount);
       pipe->winsys->buffer_reference(pipe->winsys, &ps->buffer, tex->buffer);
+      ps->format = pt->format;
       ps->cpp = pt->cpp;
       ps->width = pt->width[level];
       ps->height = pt->height[level];
index 1418af691864cb00625aa8949e427a20e25a9803..aa9362ec0baf375aa7e50becdc1b82a8bbb7f605 100644 (file)
@@ -76,16 +76,16 @@ struct pipe_winsys
                   const char *, ... ); 
 
 
-   /**
-    * flags is bitmask of PIPE_SURFACE_FLAG_RENDER, PIPE_SURFACE_FLAG_TEXTURE
-    */
-   unsigned (*surface_pitch)(struct pipe_winsys *ws, unsigned cpp,
-                            unsigned with, unsigned flags);
-
    /** allocate a new surface (no context dependency) */
-   struct pipe_surface *(*surface_alloc)(struct pipe_winsys *ws,
-                                         enum pipe_format format);
-
+   struct pipe_surface *(*surface_alloc)(struct pipe_winsys *ws);
+
+   /** allocate storage for a pipe_surface */
+   int (*surface_alloc_storage)(struct pipe_winsys *ws,
+                                struct pipe_surface *surf,
+                                unsigned width, unsigned height,
+                                enum pipe_format format,
+                                unsigned flags);
+   
    void (*surface_release)(struct pipe_winsys *ws, struct pipe_surface **s);
 
    
index 55b8b85ef2b13463c5260f6aecd734e7223e9f80..3ef3db9f1f236d5925d51799610439e412d797e2 100644 (file)
@@ -576,11 +576,11 @@ softpipe_get_tex_surface(struct pipe_context *pipe,
       assert(zslice == 0);
    }
 
-   ps = pipe->winsys->surface_alloc(pipe->winsys, pt->format);
+   ps = pipe->winsys->surface_alloc(pipe->winsys);
    if (ps) {
-      assert(ps->format);
       assert(ps->refcount);
       pipe->winsys->buffer_reference(pipe->winsys, &ps->buffer, spt->buffer);
+      ps->format = pt->format;
       ps->cpp = pt->cpp;
       ps->width = pt->width[level];
       ps->height = pt->height[level];
index b090d8927c4ff24e257644d5720105d41757c9c0..ad31f4498e53077d072e1b642dafc6cf907aae46 100644 (file)
@@ -388,11 +388,38 @@ round_up(unsigned n, unsigned multiple)
    return (n + multiple - 1) & ~(multiple - 1);
 }
 
-static unsigned
-xm_surface_pitch(struct pipe_winsys *winsys, unsigned cpp, unsigned width,
-                unsigned flags)
+static int
+xm_surface_alloc_storage(struct pipe_winsys *winsys,
+                         struct pipe_surface *surf,
+                         unsigned width, unsigned height,
+                         enum pipe_format format, 
+                         unsigned flags)
 {
-   return round_up(width, 64 / cpp);
+   const unsigned alignment = 64;
+   int ret;
+
+   surf->width = width;
+   surf->height = height;
+   surf->format = format;
+   surf->cpp = pf_get_size(format);
+   surf->pitch = round_up(width, alignment / surf->cpp);
+
+   assert(!surf->buffer);
+   surf->buffer = winsys->buffer_create(winsys, alignment, 0, 0);
+   if(!surf->buffer)
+      return -1;
+
+   ret = winsys->buffer_data(winsys, 
+                             surf->buffer,
+                             surf->pitch * surf->cpp * height,
+                             NULL,
+                             0);
+   if(ret) {
+      winsys->buffer_reference(winsys, &surf->buffer, NULL);
+      return ret;
+   }
+   
+   return 0;
 }
 
 
@@ -401,14 +428,12 @@ xm_surface_pitch(struct pipe_winsys *winsys, unsigned cpp, unsigned width,
  * renderbuffers, etc.
  */
 static struct pipe_surface *
-xm_surface_alloc(struct pipe_winsys *ws, enum pipe_format pipeFormat)
+xm_surface_alloc(struct pipe_winsys *ws)
 {
    struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
 
    assert(ws);
-   assert(pipeFormat);
 
-   xms->surface.format = pipeFormat;
    xms->surface.refcount = 1;
    xms->surface.winsys = ws;
 
@@ -463,8 +488,8 @@ xmesa_get_pipe_winsys(void)
       ws->buffer_subdata = xm_buffer_subdata;
       ws->buffer_get_subdata = xm_buffer_get_subdata;
 
-      ws->surface_pitch = xm_surface_pitch;
       ws->surface_alloc = xm_surface_alloc;
+      ws->surface_alloc_storage = xm_surface_alloc_storage;
       ws->surface_release = xm_surface_release;
 
       ws->flush_frontbuffer = xm_flush_frontbuffer;
index ef6aec6e88efcc5951311330e5c05e01dc91f63f..047de412e328da13f8779df9fd0a6867be602e54 100644 (file)
@@ -90,29 +90,15 @@ st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
    uint type = strb->screenSurface ? PIPE_SCREEN_SURFACE : PIPE_SURFACE;
    const enum pipe_format pipeFormat
       = st_choose_pipe_format(pipe, internalFormat, GL_NONE, GL_NONE, type);
-   GLuint cpp;
    GLbitfield flags = 0x0; /* XXX needed? */
 
-   cpp = init_renderbuffer_bits(strb, pipeFormat);
-   assert(cpp);
-
-   if (strb->surface && strb->surface->format != pipeFormat) {
-      /* need to change surface types, free this surface */
-      pipe_surface_reference(&strb->surface, NULL);
-      assert(strb->surface == NULL);
-   }
-
    if (!strb->surface) {
-      strb->surface = pipe->winsys->surface_alloc(pipe->winsys, pipeFormat);
+      strb->surface = pipe->winsys->surface_alloc(pipe->winsys);
       assert(strb->surface);
       if (!strb->surface)
          return GL_FALSE;
-      strb->surface->cpp = cpp;
    }
 
-   strb->surface->pitch = pipe->winsys->surface_pitch(pipe->winsys, cpp,
-                                                      width, flags);
-
    /* loop here since mapping is refcounted */
    while (strb->surface->map)
       pipe_surface_unmap(strb->surface);
@@ -120,20 +106,24 @@ st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
       pipe->winsys->buffer_reference(pipe->winsys, &strb->surface->buffer,
                                     NULL);
 
-   /* XXX don't hard-code magic 32 here */
-   strb->surface->buffer = pipe->winsys->buffer_create(pipe->winsys, 32, 0, 0);
+   pipe->winsys->surface_alloc_storage(pipe->winsys,
+                                       strb->surface,
+                                       width,
+                                       height,
+                                       pipeFormat,
+                                       flags);
    if (!strb->surface->buffer)
       return GL_FALSE; /* out of memory, try s/w buffer? */
 
-   pipe->winsys->buffer_data(pipe->winsys, strb->surface->buffer,
-                            strb->surface->pitch * cpp * height, NULL,
-                            PIPE_BUFFER_USAGE_PIXEL);
-
    ASSERT(strb->surface->buffer);
    ASSERT(strb->surface->format);
+   ASSERT(strb->surface->cpp);
+   ASSERT(strb->surface->width == width);
+   ASSERT(strb->surface->height == height);
+   ASSERT(strb->surface->pitch);
 
-   strb->Base.Width  = strb->surface->width  = width;
-   strb->Base.Height = strb->surface->height = height;
+   strb->Base.Width  = width;
+   strb->Base.Height = height;
 
    return GL_TRUE;
 }