st/xorg: Better checks for unsupported component alpha pictures.
[mesa.git] / src / gallium / state_trackers / xorg / xorg_composite.c
index 15c955450d0688ee3a2089603d37e991bb252cd0..1bfcc2886652b6f10b688f0fe1f48bb58789af1f 100644 (file)
@@ -12,9 +12,9 @@ struct xorg_composite_blend {
    int op:8;
 
    unsigned rgb_src_factor:5;    /**< PIPE_BLENDFACTOR_x */
-   unsigned rgb_dst_factor:5;    /**< PIPE_BLENDFACTOR_x */
-
    unsigned alpha_src_factor:5;  /**< PIPE_BLENDFACTOR_x */
+
+   unsigned rgb_dst_factor:5;    /**< PIPE_BLENDFACTOR_x */
    unsigned alpha_dst_factor:5;  /**< PIPE_BLENDFACTOR_x */
 };
 
@@ -150,24 +150,22 @@ setup_vertex_data0(struct exa_context *ctx,
                    int srcX, int srcY, int maskX, int maskY,
                    int dstX, int dstY, int width, int height)
 {
-   float vertices[4][2][4];
-
    /* 1st vertex */
-   setup_vertex0(vertices[0], dstX, dstY,
+   setup_vertex0(ctx->vertices2[0], dstX, dstY,
                  ctx->solid_color);
    /* 2nd vertex */
-   setup_vertex0(vertices[1], dstX + width, dstY,
+   setup_vertex0(ctx->vertices2[1], dstX + width, dstY,
                  ctx->solid_color);
    /* 3rd vertex */
-   setup_vertex0(vertices[2], dstX + width, dstY + height,
+   setup_vertex0(ctx->vertices2[2], dstX + width, dstY + height,
                  ctx->solid_color);
    /* 4th vertex */
-   setup_vertex0(vertices[3], dstX, dstY + height,
+   setup_vertex0(ctx->vertices2[3], dstX, dstY + height,
                  ctx->solid_color);
 
    return pipe_user_buffer_create(ctx->pipe->screen,
-                                  vertices,
-                                  sizeof(vertices));
+                                  ctx->vertices2,
+                                  sizeof(ctx->vertices2));
 }
 
 static INLINE void
@@ -189,7 +187,6 @@ setup_vertex_data1(struct exa_context *ctx,
                    int srcX, int srcY, int maskX, int maskY,
                    int dstX, int dstY, int width, int height)
 {
-   float vertices[4][2][4];
    float s0, t0, s1, t1;
    struct pipe_texture *src = ctx->bound_textures[0];
 
@@ -199,21 +196,21 @@ setup_vertex_data1(struct exa_context *ctx,
    t1 = srcY + height / src->height[0];
 
    /* 1st vertex */
-   setup_vertex1(vertices[0], dstX, dstY,
+   setup_vertex1(ctx->vertices2[0], dstX, dstY,
                  s0, t0);
    /* 2nd vertex */
-   setup_vertex1(vertices[1], dstX + width, dstY,
+   setup_vertex1(ctx->vertices2[1], dstX + width, dstY,
                  s1, t0);
    /* 3rd vertex */
-   setup_vertex1(vertices[2], dstX + width, dstY + height,
+   setup_vertex1(ctx->vertices2[2], dstX + width, dstY + height,
                  s1, t1);
    /* 4th vertex */
-   setup_vertex1(vertices[3], dstX, dstY + height,
+   setup_vertex1(ctx->vertices2[3], dstX, dstY + height,
                  s0, t1);
 
    return pipe_user_buffer_create(ctx->pipe->screen,
-                                  vertices,
-                                  sizeof(vertices));
+                                  ctx->vertices2,
+                                  sizeof(ctx->vertices2));
 }
 
 static struct pipe_buffer *
@@ -222,24 +219,22 @@ setup_vertex_data_tex(struct exa_context *ctx,
                       float s0, float t0, float s1, float t1,
                       float z)
 {
-   float vertices[4][2][4];
-
    /* 1st vertex */
-   setup_vertex1(vertices[0], x0, y0,
+   setup_vertex1(ctx->vertices2[0], x0, y0,
                  s0, t0);
    /* 2nd vertex */
-   setup_vertex1(vertices[1], x1, y0,
+   setup_vertex1(ctx->vertices2[1], x1, y0,
                  s1, t0);
    /* 3rd vertex */
-   setup_vertex1(vertices[2], x1, y1,
+   setup_vertex1(ctx->vertices2[2], x1, y1,
                  s1, t1);
    /* 4th vertex */
-   setup_vertex1(vertices[3], x0, y1,
+   setup_vertex1(ctx->vertices2[3], x0, y1,
                  s0, t1);
 
    return pipe_user_buffer_create(ctx->pipe->screen,
-                                  vertices,
-                                  sizeof(vertices));
+                                  ctx->vertices2,
+                                  sizeof(ctx->vertices2));
 }
 
 
@@ -269,7 +264,6 @@ setup_vertex_data2(struct exa_context *ctx,
                    int srcX, int srcY, int maskX, int maskY,
                    int dstX, int dstY, int width, int height)
 {
-   float vertices[4][3][4];
    float st0[4], st1[4];
    struct pipe_texture *src = ctx->bound_textures[0];
    struct pipe_texture *mask = ctx->bound_textures[0];
@@ -285,21 +279,21 @@ setup_vertex_data2(struct exa_context *ctx,
    st1[3] = maskY + height / mask->height[0];
 
    /* 1st vertex */
-   setup_vertex2(vertices[0], dstX, dstY,
+   setup_vertex2(ctx->vertices3[0], dstX, dstY,
                  st0[0], st0[1], st1[0], st1[1]);
    /* 2nd vertex */
-   setup_vertex2(vertices[1], dstX + width, dstY,
+   setup_vertex2(ctx->vertices3[1], dstX + width, dstY,
                  st0[2], st0[1], st1[2], st1[1]);
    /* 3rd vertex */
-   setup_vertex2(vertices[2], dstX + width, dstY + height,
+   setup_vertex2(ctx->vertices3[2], dstX + width, dstY + height,
                  st0[2], st0[3], st1[2], st1[3]);
    /* 4th vertex */
-   setup_vertex2(vertices[3], dstX, dstY + height,
+   setup_vertex2(ctx->vertices3[3], dstX, dstY + height,
                  st0[0], st0[3], st1[0], st1[3]);
 
    return pipe_user_buffer_create(ctx->pipe->screen,
-                                  vertices,
-                                  sizeof(vertices));
+                                  ctx->vertices3,
+                                  sizeof(ctx->vertices3));
 }
 
 boolean xorg_composite_accelerated(int op,
@@ -311,22 +305,15 @@ boolean xorg_composite_accelerated(int op,
    unsigned accel_ops_count =
       sizeof(accelerated_ops)/sizeof(struct acceleration_info);
 
-
-   /*FIXME: currently accel is disabled */
-   return FALSE;
-
-   if (pSrcPicture) {
-      /* component alpha not supported */
-      if (pSrcPicture->componentAlpha)
-         return FALSE;
-      /* fills not supported */
-      if (pSrcPicture->pSourcePict)
-         return FALSE;
-   }
-
    for (i = 0; i < accel_ops_count; ++i) {
       if (op == accelerated_ops[i].op) {
-         if (pMaskPicture && !accelerated_ops[i].with_mask)
+         /* Check for unsupported component alpha */
+         if ((pSrcPicture->componentAlpha &&
+              !accelerated_ops[i].component_alpha) ||
+             (pMaskPicture &&
+              (!accelerated_ops[i].with_mask ||
+               (pMaskPicture->componentAlpha &&
+                !accelerated_ops[i].component_alpha))))
             return FALSE;
          return TRUE;
       }
@@ -359,6 +346,9 @@ bind_framebuffer_state(struct exa_context *exa, struct exa_pixmap_priv *pDst)
    state.zsbuf = 0;
 
    cso_set_framebuffer(exa->cso, &state);
+
+   /* we do fire and forget for the framebuffer, this is the forget part */
+   pipe_surface_reference(&surface, NULL);
 }
 
 enum AxisOrientation {
@@ -391,7 +381,7 @@ bind_viewport_state(struct exa_context *exa, struct exa_pixmap_priv *pDst)
    int width = pDst->tex->width[0];
    int height = pDst->tex->height[0];
 
-   debug_printf("Bind viewport (%d, %d)\n", width, height);
+   /*debug_printf("Bind viewport (%d, %d)\n", width, height);*/
 
    set_viewport(exa, width, height, Y0_TOP);
 }
@@ -400,14 +390,9 @@ static void
 bind_blend_state(struct exa_context *exa, int op,
                  PicturePtr pSrcPicture, PicturePtr pMaskPicture)
 {
-   boolean component_alpha = (pSrcPicture) ?
-                             pSrcPicture->componentAlpha : FALSE;
    struct xorg_composite_blend blend_opt;
    struct pipe_blend_state blend;
 
-   if (component_alpha) {
-      op = PictOpOver;
-   }
    blend_opt = blend_for_op(op);
 
    memset(&blend, 0, sizeof(struct pipe_blend_state));
@@ -653,17 +638,12 @@ boolean xorg_solid_bind_state(struct exa_context *exa,
 
    exa->solid_color[3] = 1.f;
 
+#if 0
    debug_printf("Color Pixel=(%d, %d, %d, %d), RGBA=(%f, %f, %f, %f)\n",
                 (fg >> 24) & 0xff, (fg >> 16) & 0xff,
                 (fg >> 8) & 0xff,  (fg >> 0) & 0xff,
                 exa->solid_color[0], exa->solid_color[1],
                 exa->solid_color[2], exa->solid_color[3]);
-
-#if 0
-   exa->solid_color[0] = 1.f;
-   exa->solid_color[1] = 0.f;
-   exa->solid_color[2] = 0.f;
-   exa->solid_color[3] = 1.f;
 #endif
 
    vs_traits = VS_SOLID_FILL;
@@ -680,7 +660,7 @@ boolean xorg_solid_bind_state(struct exa_context *exa,
    cso_set_vertex_shader_handle(exa->cso, shader.vs);
    cso_set_fragment_shader_handle(exa->cso, shader.fs);
 
-   return FALSE;
+   return TRUE;
 }
 
 void xorg_solid(struct exa_context *exa,
@@ -689,31 +669,26 @@ void xorg_solid(struct exa_context *exa,
 {
    struct pipe_context *pipe = exa->pipe;
    struct pipe_buffer *buf = 0;
-   float vertices[4][2][4];
-
-   x0 = 10; y0 = 10;
-   x1 = 300; y1 = 300;
 
    /* 1st vertex */
-   setup_vertex0(vertices[0], x0, y0,
+   setup_vertex0(exa->vertices2[0], x0, y0,
                  exa->solid_color);
    /* 2nd vertex */
-   setup_vertex0(vertices[1], x1, y0,
+   setup_vertex0(exa->vertices2[1], x1, y0,
                  exa->solid_color);
    /* 3rd vertex */
-   setup_vertex0(vertices[2], x1, y1,
+   setup_vertex0(exa->vertices2[2], x1, y1,
                  exa->solid_color);
    /* 4th vertex */
-   setup_vertex0(vertices[3], x0, y1,
+   setup_vertex0(exa->vertices2[3], x0, y1,
                  exa->solid_color);
 
    buf = pipe_user_buffer_create(exa->pipe->screen,
-                                 vertices,
-                                 sizeof(vertices));
+                                 exa->vertices2,
+                                 sizeof(exa->vertices2));
 
 
    if (buf) {
-      debug_printf("Drawing buf is %p\n", buf);
       util_draw_vertex_buffer(pipe, buf, 0,
                               PIPE_PRIM_TRIANGLE_FAN,
                               4,  /* verts */
@@ -844,11 +819,6 @@ static void renderer_copy_texture(struct exa_context *exa,
    assert(dst->width[0] != 0);
    assert(dst->height[0] != 0);
 
-#if 0
-   debug_printf("copy texture [%f, %f, %f, %f], [%f, %f, %f, %f]\n",
-                sx1, sy1, sx2, sy2, dx1, dy1, dx2, dy2);
-#endif
-
 #if 1
    s0 = sx1 / src->width[0];
    s1 = sx2 / src->width[0];
@@ -861,8 +831,16 @@ static void renderer_copy_texture(struct exa_context *exa,
    t1 = 1;
 #endif
 
-   assert(screen->is_format_supported(screen, dst_surf->format, PIPE_TEXTURE_2D,
-                                      PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
+#if 1
+   debug_printf("copy texture src=[%f, %f, %f, %f], dst=[%f, %f, %f, %f], tex=[%f, %f, %f, %f]\n",
+                sx1, sy1, sx2, sy2, dx1, dy1, dx2, dy2,
+                s0, t0, s1, t1);
+#endif
+
+   assert(screen->is_format_supported(screen, dst_surf->format,
+                                      PIPE_TEXTURE_2D,
+                                      PIPE_TEXTURE_USAGE_RENDER_TARGET,
+                                      0));
 
    /* save state (restored below) */
    cso_save_blend(exa->cso);
@@ -926,6 +904,8 @@ static void renderer_copy_texture(struct exa_context *exa,
          fb.cbufs[i] = 0;
    }
    cso_set_framebuffer(exa->cso, &fb);
+   setup_vs_constant_buffer(exa, fb.width, fb.height);
+   setup_fs_constant_buffer(exa);
 
    /* draw quad */
    buf = setup_vertex_data_tex(exa,
@@ -955,6 +935,61 @@ static void renderer_copy_texture(struct exa_context *exa,
    pipe_surface_reference(&dst_surf, NULL);
 }
 
+
+static struct pipe_texture *
+create_sampler_texture(struct exa_context *ctx,
+                       struct pipe_texture *src)
+{
+   enum pipe_format format;
+   struct pipe_context *pipe = ctx->pipe;
+   struct pipe_screen *screen = pipe->screen;
+   struct pipe_texture *pt;
+   struct pipe_texture templ;
+
+   pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
+
+   /* the coming in texture should already have that invariance */
+   debug_assert(screen->is_format_supported(screen, src->format,
+                                            PIPE_TEXTURE_2D,
+                                            PIPE_TEXTURE_USAGE_SAMPLER, 0));
+
+   format = src->format;
+
+   memset(&templ, 0, sizeof(templ));
+   templ.target = PIPE_TEXTURE_2D;
+   templ.format = format;
+   templ.last_level = 0;
+   templ.width[0] = src->width[0];
+   templ.height[0] = src->height[0];
+   templ.depth[0] = 1;
+   pf_get_block(format, &templ.block);
+   templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
+
+   pt = screen->texture_create(screen, &templ);
+
+   debug_assert(!pt || pipe_is_referenced(&pt->reference));
+
+   if (!pt)
+      return NULL;
+
+   {
+      /* copy source framebuffer surface into texture */
+      struct pipe_surface *ps_read = screen->get_tex_surface(
+         screen, src, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ);
+      struct pipe_surface *ps_tex = screen->get_tex_surface(
+         screen, pt, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE );
+      pipe->surface_copy(pipe,
+                        ps_tex, /* dest */
+                        0, 0, /* destx/y */
+                        ps_read,
+                        0, 0, src->width[0], src->height[0]);
+      pipe_surface_reference(&ps_read, NULL);
+      pipe_surface_reference(&ps_tex, NULL);
+   }
+
+   return pt;
+}
+
 void xorg_copy_pixmap(struct exa_context *ctx,
                       struct exa_pixmap_priv *dst_priv, int dx, int dy,
                       struct exa_pixmap_priv *src_priv, int sx, int sy,
@@ -966,6 +1001,8 @@ void xorg_copy_pixmap(struct exa_context *ctx,
    struct pipe_texture *dst = dst_priv->tex;
    struct pipe_texture *src = src_priv->tex;
 
+   xorg_exa_finish(ctx);
+
    dst_loc[0] = dx;
    dst_loc[1] = dy;
    dst_loc[2] = width;
@@ -1003,17 +1040,25 @@ void xorg_copy_pixmap(struct exa_context *ctx,
 
    if (src_loc[2] >= 0 && src_loc[3] >= 0 &&
        dst_loc[2] >= 0 && dst_loc[3] >= 0) {
+      struct pipe_texture *temp_src = src;
+
+      if (src == dst)
+         temp_src = create_sampler_texture(ctx, src);
+
       renderer_copy_texture(ctx,
-                            src,
+                            temp_src,
                             src_loc[0],
-                            src_loc[1] + src_loc[3],
-                            src_loc[0] + src_loc[2],
                             src_loc[1],
+                            src_loc[0] + src_loc[2],
+                            src_loc[1] + src_loc[3],
                             dst,
                             dst_loc[0],
-                            dst_loc[1] + dst_loc[3],
+                            dst_loc[1],
                             dst_loc[0] + dst_loc[2],
-                            dst_loc[1]);
+                            dst_loc[1] + dst_loc[3]);
+
+      if (src == dst)
+         pipe_texture_reference(&temp_src, NULL);
    }
 }