intel / DRI2: When available, use DRI2GetBuffersWithFormat
[mesa.git] / src / mesa / state_tracker / st_cb_bitmap.c
index 93f1f0baacff47df30c6630e1cde1a0dce005cee..8709633557cc6bc4a66e34b959d25f266add23d6 100644 (file)
@@ -47,6 +47,8 @@
 #include "st_cb_program.h"
 #include "st_mesa_to_tgsi.h"
 #include "st_texture.h"
+#include "st_inlines.h"
+
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
 #include "pipe/p_inlines.h"
@@ -142,8 +144,12 @@ make_bitmap_fragment_program(GLcontext *ctx, GLuint samplerIndex)
    /* KIL if -tmp0 < 0 # texel=0 -> keep / texel=0 -> discard */
    p->Instructions[ic].Opcode = OPCODE_KIL;
    p->Instructions[ic].SrcReg[0].File = PROGRAM_TEMPORARY;
+
+   if (ctx->st->bitmap.tex_format == PIPE_FORMAT_L8_UNORM)
+      p->Instructions[ic].SrcReg[0].Swizzle = SWIZZLE_XXXX;
+
    p->Instructions[ic].SrcReg[0].Index = 0;
-   p->Instructions[ic].SrcReg[0].NegateBase = NEGATE_XYZW;
+   p->Instructions[ic].SrcReg[0].Negate = NEGATE_XYZW;
    ic++;
 
    /* END; */
@@ -157,7 +163,11 @@ make_bitmap_fragment_program(GLcontext *ctx, GLuint samplerIndex)
 
    stfp = (struct st_fragment_program *) p;
    stfp->Base.UsesKill = GL_TRUE;
-   st_translate_fragment_program(ctx->st, stfp, NULL);
+
+   /* No need to send this incomplete program down to hardware:
+    *
+    * st_translate_fragment_program(ctx->st, stfp, NULL);
+    */
 
    return stfp;
 }
@@ -322,15 +332,16 @@ make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
     * Create texture to hold bitmap pattern.
     */
    pt = st_texture_create(ctx->st, PIPE_TEXTURE_2D, ctx->st->bitmap.tex_format,
-                          0, width, height, 1, 0,
+                          0, width, height, 1,
                           PIPE_TEXTURE_USAGE_SAMPLER);
    if (!pt) {
       _mesa_unmap_bitmap_pbo(ctx, unpack);
       return NULL;
    }
 
-   transfer = screen->get_tex_transfer(screen, pt, 0, 0, 0, PIPE_TRANSFER_WRITE,
-                                       0, 0, width, height);
+   transfer = st_no_flush_get_tex_transfer(st_context(ctx), pt, 0, 0, 0,
+                                          PIPE_TRANSFER_WRITE,
+                                          0, 0, width, height);
 
    dest = screen->transfer_map(screen, transfer);
 
@@ -343,7 +354,7 @@ make_bitmap_texture(GLcontext *ctx, GLsizei width, GLsizei height,
 
    /* Release transfer */
    screen->transfer_unmap(screen, transfer);
-   screen->tex_transfer_release(screen, &transfer);
+   screen->tex_transfer_destroy(transfer);
 
    return pt;
 }
@@ -371,7 +382,7 @@ setup_bitmap_vertex_data(struct st_context *st,
    GLuint i;
 
    if (st->bitmap.vbuf_slot >= max_slots) {
-      pipe_buffer_reference(pipe->screen, &st->bitmap.vbuf, NULL);
+      pipe_buffer_reference(&st->bitmap.vbuf, NULL);
       st->bitmap.vbuf_slot = 0;
    }
 
@@ -417,17 +428,11 @@ setup_bitmap_vertex_data(struct st_context *st,
    }
 
    /* put vertex data into vbuf */
-   {
-      char *buf = pipe_buffer_map(pipe->screen, 
-                                  st->bitmap.vbuf, 
-                                  PIPE_BUFFER_USAGE_CPU_WRITE);
-
-      memcpy(buf + st->bitmap.vbuf_slot * sizeof st->bitmap.vertices, 
-             st->bitmap.vertices, 
-             sizeof st->bitmap.vertices);
-
-      pipe_buffer_unmap(pipe->screen, st->bitmap.vbuf);
-   }
+   st_no_flush_pipe_buffer_write(st,
+                                st->bitmap.vbuf,
+                                st->bitmap.vbuf_slot * sizeof st->bitmap.vertices,
+                                sizeof st->bitmap.vertices,
+                                st->bitmap.vertices);
 
    return st->bitmap.vbuf_slot++ * sizeof st->bitmap.vertices;
 }
@@ -568,8 +573,10 @@ reset_cache(struct st_context *st)
    cache->ymin = 1000000;
    cache->ymax = -1000000;
 
-   if (cache->trans)
-      screen->tex_transfer_release(screen, &cache->trans);
+   if (cache->trans) {
+      screen->tex_transfer_destroy(cache->trans);
+      cache->trans = NULL;
+   }
 
    assert(!cache->texture);
 
@@ -577,16 +584,27 @@ reset_cache(struct st_context *st)
    cache->texture = st_texture_create(st, PIPE_TEXTURE_2D,
                                       st->bitmap.tex_format, 0,
                                       BITMAP_CACHE_WIDTH, BITMAP_CACHE_HEIGHT,
-                                      1, 0,
-                                      PIPE_TEXTURE_USAGE_SAMPLER);
+                                      1, PIPE_TEXTURE_USAGE_SAMPLER);
+
+}
+
+static void
+create_cache_trans(struct st_context *st)
+{
+   struct pipe_context *pipe = st->pipe;
+   struct pipe_screen *screen = pipe->screen;
+   struct bitmap_cache *cache = st->bitmap.cache;
+
+   if (cache->trans)
+      return;
 
    /* Map the texture transfer.
     * Subsequent glBitmap calls will write into the texture image.
     */
-   cache->trans = screen->get_tex_transfer(screen, cache->texture, 0, 0, 0,
-                                           PIPE_TRANSFER_WRITE, 0, 0,
-                                           BITMAP_CACHE_WIDTH,
-                                           BITMAP_CACHE_HEIGHT);
+   cache->trans = st_no_flush_get_tex_transfer(st, cache->texture, 0, 0, 0,
+                                              PIPE_TRANSFER_WRITE, 0, 0,
+                                              BITMAP_CACHE_WIDTH,
+                                              BITMAP_CACHE_HEIGHT);
    cache->buffer = screen->transfer_map(screen, cache->trans);
 
    /* init image to all 0xff */
@@ -618,10 +636,13 @@ st_flush_bitmap_cache(struct st_context *st)
          /* The texture transfer has been mapped until now.
           * So unmap and release the texture transfer before drawing.
           */
-         screen->transfer_unmap(screen, cache->trans);
-         cache->buffer = NULL;
+         if (cache->trans) {
+            screen->transfer_unmap(screen, cache->trans);
+            cache->buffer = NULL;
 
-         screen->tex_transfer_release(screen, &cache->trans);
+            screen->tex_transfer_destroy(cache->trans);
+            cache->trans = NULL;
+         }
 
          draw_bitmap_quad(st->ctx,
                           cache->xpos,
@@ -649,7 +670,7 @@ st_flush_bitmap( struct st_context *st )
    /* Release vertex buffer to avoid synchronous rendering if we were
     * to map it in the next frame.
     */
-   pipe_buffer_reference(st->pipe->screen, &st->bitmap.vbuf, NULL);
+   pipe_buffer_reference(&st->bitmap.vbuf, NULL);
    st->bitmap.vbuf_slot = 0;
 }
 
@@ -706,6 +727,9 @@ accum_bitmap(struct st_context *st,
    if (y + height > cache->ymax)
       cache->ymax = y + height;
 
+   /* create the transfer if needed */
+   create_cache_trans(st);
+
    unpack_bitmap(st, px, py, width, height, unpack, bitmap,
                  cache->buffer, BITMAP_CACHE_WIDTH);
 
@@ -737,8 +761,7 @@ st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
       const uint semantic_indexes[] = { 0, 0, 0 };
       st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
                                                           semantic_names,
-                                                          semantic_indexes,
-                                                          &st->bitmap.vert_shader);
+                                                          semantic_indexes);
    }
 
    if (UseBitmapCache && accum_bitmap(st, x, y, width, height, unpack, bitmap))
@@ -785,20 +808,27 @@ st_init_bitmap(struct st_context *st)
    /* init baseline rasterizer state once */
    memset(&st->bitmap.rasterizer, 0, sizeof(st->bitmap.rasterizer));
    st->bitmap.rasterizer.gl_rasterization_rules = 1;
-   st->bitmap.rasterizer.bypass_vs = 1;
 
    /* find a usable texture format */
    if (screen->is_format_supported(screen, PIPE_FORMAT_I8_UNORM, PIPE_TEXTURE_2D, 
                                    PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
       st->bitmap.tex_format = PIPE_FORMAT_I8_UNORM;
    }
+   else if (screen->is_format_supported(screen, PIPE_FORMAT_A8_UNORM, PIPE_TEXTURE_2D, 
+                                        PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
+      st->bitmap.tex_format = PIPE_FORMAT_A8_UNORM;
+   }
+   else if (screen->is_format_supported(screen, PIPE_FORMAT_L8_UNORM, PIPE_TEXTURE_2D, 
+                                        PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
+      st->bitmap.tex_format = PIPE_FORMAT_L8_UNORM;
+   }
    else {
       /* XXX support more formats */
       assert(0);
    }
 
    /* alloc bitmap cache object */
-   st->bitmap.cache = CALLOC_STRUCT(bitmap_cache);
+   st->bitmap.cache = ST_CALLOC_STRUCT(bitmap_cache);
 
    reset_cache(st);
 }
@@ -812,8 +842,7 @@ st_destroy_bitmap(struct st_context *st)
    struct pipe_screen *screen = pipe->screen;
    struct bitmap_cache *cache = st->bitmap.cache;
 
-   screen->transfer_unmap(screen, cache->trans);
-   screen->tex_transfer_release(screen, &cache->trans);
+
 
    if (st->bitmap.vs) {
       cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
@@ -821,13 +850,17 @@ st_destroy_bitmap(struct st_context *st)
    }
 
    if (st->bitmap.vbuf) {
-      pipe_buffer_reference(pipe->screen, &st->bitmap.vbuf, NULL);
+      pipe_buffer_reference(&st->bitmap.vbuf, NULL);
       st->bitmap.vbuf = NULL;
    }
 
-   if (st->bitmap.cache) {
-      pipe_texture_release(&st->bitmap.cache->texture);
-      FREE(st->bitmap.cache);
+   if (cache) {
+      if (cache->trans) {
+         screen->transfer_unmap(screen, cache->trans);
+         screen->tex_transfer_destroy(cache->trans);
+      }
+      pipe_texture_reference(&st->bitmap.cache->texture, NULL);
+      _mesa_free(st->bitmap.cache);
       st->bitmap.cache = NULL;
    }
 }