[g3dvl] move compositor creation and handling directly into the state trackers
[mesa.git] / src / gallium / state_trackers / vega / image.c
index 9c323b1809cc4646f83bccde6cf71bd7522372d5..44480876b6e7f2fb9db62687c021b6fdccd251a6 100644 (file)
 #include "renderer.h"
 #include "util_array.h"
 #include "api_consts.h"
-#include "shaders_cache.h"
 #include "shader.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_screen.h"
 #include "util/u_inlines.h"
-#include "util/u_blit.h"
 #include "util/u_format.h"
 #include "util/u_tile.h"
 #include "util/u_memory.h"
 #include "util/u_math.h"
 #include "util/u_sampler.h"
+#include "util/u_surface.h"
 
 static enum pipe_format vg_format_to_pipe(VGImageFormat format)
 {
@@ -79,33 +78,23 @@ static INLINE void vg_sync_size(VGfloat *src_loc, VGfloat *dst_loc)
    dst_loc[3] = src_loc[3];
 }
 
-
-static void vg_copy_texture(struct vg_context *ctx,
-                            struct pipe_resource *dst, VGint dx, VGint dy,
-                            struct pipe_sampler_view *src, VGint sx, VGint sy,
-                            VGint width, VGint height)
+static void vg_get_copy_coords(VGfloat *src_loc,
+                               VGfloat src_width, VGfloat src_height,
+                               VGfloat *dst_loc,
+                               VGfloat dst_width, VGfloat dst_height)
 {
-   VGfloat dst_loc[4], src_loc[4];
    VGfloat dst_bounds[4], src_bounds[4];
    VGfloat src_shift[4], dst_shift[4], shift[4];
 
-   dst_loc[0] = dx;
-   dst_loc[1] = dy;
-   dst_loc[2] = width;
-   dst_loc[3] = height;
    dst_bounds[0] = 0.f;
    dst_bounds[1] = 0.f;
-   dst_bounds[2] = dst->width0;
-   dst_bounds[3] = dst->height0;
+   dst_bounds[2] = dst_width;
+   dst_bounds[3] = dst_height;
 
-   src_loc[0] = sx;
-   src_loc[1] = sy;
-   src_loc[2] = width;
-   src_loc[3] = height;
    src_bounds[0] = 0.f;
    src_bounds[1] = 0.f;
-   src_bounds[2] = src->texture->width0;
-   src_bounds[3] = src->texture->height0;
+   src_bounds[2] = src_width;
+   src_bounds[3] = src_height;
 
    vg_bound_rect(src_loc, src_bounds, src_shift);
    vg_bound_rect(dst_loc, dst_bounds, dst_shift);
@@ -123,22 +112,44 @@ static void vg_copy_texture(struct vg_context *ctx,
       vg_shift_recty(dst_loc, dst_bounds, shift[1]);
 
    vg_sync_size(src_loc, dst_loc);
+}
+
+static void vg_copy_texture(struct vg_context *ctx,
+                            struct pipe_resource *dst, VGint dx, VGint dy,
+                            struct pipe_sampler_view *src, VGint sx, VGint sy,
+                            VGint width, VGint height)
+{
+   VGfloat dst_loc[4], src_loc[4];
+
+   dst_loc[0] = dx;
+   dst_loc[1] = dy;
+   dst_loc[2] = width;
+   dst_loc[3] = height;
+
+   src_loc[0] = sx;
+   src_loc[1] = sy;
+   src_loc[2] = width;
+   src_loc[3] = height;
+
+   vg_get_copy_coords(src_loc, src->texture->width0, src->texture->height0,
+                      dst_loc, dst->width0, dst->height0);
 
    if (src_loc[2] >= 0 && src_loc[3] >= 0 &&
        dst_loc[2] >= 0 && dst_loc[3] >= 0) {
-      renderer_copy_texture(ctx->renderer,
-                            src,
-                            src_loc[0],
-                            src_loc[1] + src_loc[3],
-                            src_loc[0] + src_loc[2],
-                            src_loc[1],
-                            dst,
-                            dst_loc[0],
-                            dst_loc[1] + dst_loc[3],
-                            dst_loc[0] + dst_loc[2],
-                            dst_loc[1]);
-   }
+      struct pipe_surface *surf, surf_tmpl;
+
+      /* get the destination surface */
+      u_surface_default_template(&surf_tmpl, dst, PIPE_BIND_RENDER_TARGET);
+      surf = ctx->pipe->create_surface(ctx->pipe, dst, &surf_tmpl);
+      if (surf && renderer_copy_begin(ctx->renderer, surf, VG_TRUE, src)) {
+         renderer_copy(ctx->renderer,
+               dst_loc[0], dst_loc[1], dst_loc[2], dst_loc[3],
+               src_loc[0], src_loc[1], src_loc[2], src_loc[3]);
+         renderer_copy_end(ctx->renderer);
+      }
 
+      pipe_surface_reference(&surf, NULL);
+   }
 }
 
 void vg_copy_surface(struct vg_context *ctx,
@@ -147,43 +158,19 @@ void vg_copy_surface(struct vg_context *ctx,
                      VGint width, VGint height)
 {
    VGfloat dst_loc[4], src_loc[4];
-   VGfloat dst_bounds[4], src_bounds[4];
-   VGfloat src_shift[4], dst_shift[4], shift[4];
 
    dst_loc[0] = dx;
    dst_loc[1] = dy;
    dst_loc[2] = width;
    dst_loc[3] = height;
-   dst_bounds[0] = 0.f;
-   dst_bounds[1] = 0.f;
-   dst_bounds[2] = dst->width;
-   dst_bounds[3] = dst->height;
 
    src_loc[0] = sx;
    src_loc[1] = sy;
    src_loc[2] = width;
    src_loc[3] = height;
-   src_bounds[0] = 0.f;
-   src_bounds[1] = 0.f;
-   src_bounds[2] = src->width;
-   src_bounds[3] = src->height;
 
-   vg_bound_rect(src_loc, src_bounds, src_shift);
-   vg_bound_rect(dst_loc, dst_bounds, dst_shift);
-   shift[0] = src_shift[0] - dst_shift[0];
-   shift[1] = src_shift[1] - dst_shift[1];
-
-   if (shift[0] < 0)
-      vg_shift_rectx(src_loc, src_bounds, -shift[0]);
-   else
-      vg_shift_rectx(dst_loc, dst_bounds, shift[0]);
-
-   if (shift[1] < 0)
-      vg_shift_recty(src_loc, src_bounds, -shift[1]);
-   else
-      vg_shift_recty(dst_loc, dst_bounds, shift[1]);
-
-   vg_sync_size(src_loc, dst_loc);
+   vg_get_copy_coords(src_loc, src->width, src->height,
+                      dst_loc, dst->width, dst->height);
 
    if (src_loc[2] > 0 && src_loc[3] > 0 &&
        dst_loc[2] > 0 && dst_loc[3] > 0) {
@@ -270,7 +257,7 @@ struct vg_image * image_create(VGImageFormat format,
    image->sampler.normalized_coords = 1;
 
    assert(screen->is_format_supported(screen, pformat, PIPE_TEXTURE_2D,
-                                      PIPE_BIND_SAMPLER_VIEW, 0));
+                                      0, PIPE_BIND_SAMPLER_VIEW));
 
    memset(&pt, 0, sizeof(pt));
    pt.target = PIPE_TEXTURE_2D;
@@ -279,6 +266,7 @@ struct vg_image * image_create(VGImageFormat format,
    pt.width0 = width;
    pt.height0 = height;
    pt.depth0 = 1;
+   pt.array_size = 1;
    pt.bind = PIPE_BIND_SAMPLER_VIEW;
 
    newtex = screen->resource_create(screen, &pt);
@@ -286,6 +274,13 @@ struct vg_image * image_create(VGImageFormat format,
    debug_assert(newtex);
 
    u_sampler_view_default_template(&view_templ, newtex, newtex->format);
+   /* R, G, and B are treated as 1.0 for alpha-only formats in OpenVG */
+   if (newtex->format == PIPE_FORMAT_A8_UNORM) {
+      view_templ.swizzle_r = PIPE_SWIZZLE_ONE;
+      view_templ.swizzle_g = PIPE_SWIZZLE_ONE;
+      view_templ.swizzle_b = PIPE_SWIZZLE_ONE;
+   }
+
    view = pipe->create_sampler_view(pipe, newtex, &view_templ);
    /* want the texture to go away if the view is freed */
    pipe_resource_reference(&newtex, NULL);
@@ -354,8 +349,10 @@ void image_destroy(struct vg_image *img)
       array_destroy(img->children_array);
    }
 
+   vg_free_object(&img->base);
+
    pipe_sampler_view_reference(&img->sampler_view, NULL);
-   free(img);
+   FREE(img);
 }
 
 void image_clear(struct vg_image *img,
@@ -422,7 +419,7 @@ void image_sub_data(struct vg_image *image,
 
    { /* upload color_data */
       struct pipe_transfer *transfer = pipe_get_transfer(
-         pipe, texture, 0, 0, 0,
+         pipe, texture, 0, 0,
          PIPE_TRANSFER_WRITE, 0, 0, texture->width0, texture->height0);
       src += (dataStride * yoffset);
       for (i = 0; i < height; i++) {
@@ -453,11 +450,11 @@ void image_get_sub_data(struct vg_image * image,
    {
       struct pipe_transfer *transfer =
          pipe_get_transfer(pipe,
-                                  image->sampler_view->texture,  0, 0, 0,
-                                  PIPE_TRANSFER_READ,
-                                  0, 0,
-                                  image->x + image->width,
-                                  image->y + image->height);
+                           image->sampler_view->texture,  0, 0,
+                           PIPE_TRANSFER_READ,
+                           0, 0,
+                           image->x + image->width,
+                           image->y + image->height);
       /* Do a row at a time to flip image data vertically */
       for (i = 0; i < height; i++) {
 #if 0
@@ -521,20 +518,24 @@ void image_copy(struct vg_image *dst, VGint dx, VGint dy,
       vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
       return;
    }
-   /* make sure rendering has completed */
-   ctx->pipe->flush(ctx->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
    vg_copy_texture(ctx, dst->sampler_view->texture, dst->x + dx, dst->y + dy,
                    src->sampler_view, src->x + sx, src->y + sy, width, height);
 }
 
-void image_draw(struct vg_image *img)
+void image_draw(struct vg_image *img, struct matrix *matrix)
 {
    struct vg_context *ctx = vg_current_context();
+   struct matrix paint_matrix;
    VGfloat x1, y1;
    VGfloat x2, y2;
    VGfloat x3, y3;
    VGfloat x4, y4;
-   struct matrix *matrix;
+
+   if (!vg_get_paint_matrix(ctx,
+                            &ctx->state.vg.fill_paint_to_user_matrix,
+                            matrix,
+                            &paint_matrix))
+      return;
 
    x1 = 0;
    y1 = 0;
@@ -545,15 +546,10 @@ void image_draw(struct vg_image *img)
    x4 = 0;
    y4 = img->height;
 
-   matrix = &ctx->state.vg.image_user_to_surface_matrix;
-
-   matrix_map_point(matrix, x1, y1, &x1, &y1);
-   matrix_map_point(matrix, x2, y2, &x2, &y2);
-   matrix_map_point(matrix, x3, y3, &x3, &y3);
-   matrix_map_point(matrix, x4, y4, &x4, &y4);
-
+   shader_set_surface_matrix(ctx->shader, matrix);
    shader_set_drawing_image(ctx->shader, VG_TRUE);
    shader_set_paint(ctx->shader, ctx->state.vg.fill_paint);
+   shader_set_paint_matrix(ctx->shader, &paint_matrix);
    shader_set_image(ctx->shader, img);
    shader_bind(ctx->shader);
 
@@ -568,20 +564,18 @@ void image_set_pixels(VGint dx, VGint dy,
 {
    struct vg_context *ctx = vg_current_context();
    struct pipe_context *pipe = ctx->pipe;
-   struct pipe_screen *screen = pipe->screen;
-   struct pipe_surface *surf;
+   struct pipe_surface *surf, surf_tmpl;
    struct st_renderbuffer *strb = ctx->draw_buffer->strb;
 
-   /* make sure rendering has completed */
-   pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
-
-   surf = screen->get_tex_surface(screen, image_texture(src),  0, 0, 0,
-                                  PIPE_BIND_BLIT_SOURCE);
+   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
+   u_surface_default_template(&surf_tmpl, image_texture(src),
+                              0 /* no bind flag - not a surface*/);
+   surf = pipe->create_surface(pipe, image_texture(src), &surf_tmpl);
 
    vg_copy_surface(ctx, strb->surface, dx, dy,
                    surf, sx+src->x, sy+src->y, width, height);
 
-   screen->tex_surface_destroy(surf);
+   pipe->surface_destroy(pipe, surf);
 }
 
 void image_get_pixels(struct vg_image *dst, VGint dx, VGint dy,
@@ -590,18 +584,16 @@ void image_get_pixels(struct vg_image *dst, VGint dx, VGint dy,
 {
    struct vg_context *ctx = vg_current_context();
    struct pipe_context *pipe = ctx->pipe;
-   struct pipe_screen *screen = pipe->screen;
-   struct pipe_surface *surf;
+   struct pipe_surface *surf, surf_tmpl;
    struct st_renderbuffer *strb = ctx->draw_buffer->strb;
 
    /* flip the y coordinates */
    /*dy = dst->height - dy - height;*/
 
-   /* make sure rendering has completed */
-   pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
-
-   surf = screen->get_tex_surface(screen, image_texture(dst),  0, 0, 0,
-                                  PIPE_BIND_BLIT_SOURCE);
+   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
+   u_surface_default_template(&surf_tmpl, image_texture(dst),
+                              PIPE_BIND_RENDER_TARGET);
+   surf = pipe->create_surface(pipe, image_texture(dst), &surf_tmpl);
 
    vg_copy_surface(ctx, surf, dst->x + dx, dst->y + dy,
                    strb->surface, sx, sy, width, height);