vl: replace decode_buffers with auxiliary data field
[mesa.git] / src / gallium / auxiliary / vl / vl_compositor.c
index faca96dc55b0768f7c85fdf9e819e40c693f47a9..3631145b3b5c4decaf5d76774c9b8580966830e1 100644 (file)
 
 #include <assert.h>
 
-#include <pipe/p_compiler.h>
-#include <pipe/p_context.h>
+#include "pipe/p_compiler.h"
+#include "pipe/p_context.h"
 
-#include <util/u_memory.h>
-#include <util/u_draw.h>
-#include <util/u_surface.h>
+#include "util/u_memory.h"
+#include "util/u_draw.h"
+#include "util/u_surface.h"
 
-#include <tgsi/tgsi_ureg.h>
+#include "tgsi/tgsi_ureg.h"
 
 #include "vl_csc.h"
 #include "vl_types.h"
 #include "vl_compositor.h"
 
+#define MIN_DIRTY (0)
+#define MAX_DIRTY (1 << 15)
+
 typedef float csc_matrix[16];
 
 static void *
@@ -114,7 +117,7 @@ create_frag_shader_video_buffer(struct vl_compositor *c)
 }
 
 static void *
-create_frag_shader_palette(struct vl_compositor *c)
+create_frag_shader_palette(struct vl_compositor *c, bool include_cc)
 {
    struct ureg_program *shader;
    struct ureg_src csc[3];
@@ -129,12 +132,13 @@ create_frag_shader_palette(struct vl_compositor *c)
    if (!shader)
       return false;
 
-   for (i = 0; i < 3; ++i)
+   for (i = 0; include_cc && i < 3; ++i)
       csc[i] = ureg_DECL_constant(shader, i);
 
    tc = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, 1, TGSI_INTERPOLATE_LINEAR);
    sampler = ureg_DECL_sampler(shader, 0);
    palette = ureg_DECL_sampler(shader, 1);
+
    texel = ureg_DECL_temporary(shader);
    fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
 
@@ -144,13 +148,16 @@ create_frag_shader_palette(struct vl_compositor *c)
     * fragment.a = texel.a
     */
    ureg_TEX(shader, texel, TGSI_TEXTURE_2D, tc, sampler);
-   ureg_MUL(shader, ureg_writemask(texel, TGSI_WRITEMASK_X), ureg_src(texel), ureg_imm1f(shader, 15.0f / 16.0f));
    ureg_MOV(shader, ureg_writemask(fragment, TGSI_WRITEMASK_W), ureg_src(texel));
 
-   ureg_TEX(shader, texel, TGSI_TEXTURE_1D, ureg_src(texel), palette);
-
-   for (i = 0; i < 3; ++i)
-      ureg_DP4(shader, ureg_writemask(fragment, TGSI_WRITEMASK_X << i), csc[i], ureg_src(texel));
+   if (include_cc) {
+      ureg_TEX(shader, texel, TGSI_TEXTURE_1D, ureg_src(texel), palette);
+      for (i = 0; i < 3; ++i)
+         ureg_DP4(shader, ureg_writemask(fragment, TGSI_WRITEMASK_X << i), csc[i], ureg_src(texel));
+   } else {
+      ureg_TEX(shader, ureg_writemask(fragment, TGSI_WRITEMASK_XYZ),
+               TGSI_TEXTURE_1D, ureg_src(texel), palette);
+   }
 
    ureg_release_temporary(shader, texel);
    ureg_END(shader);
@@ -200,9 +207,15 @@ init_shaders(struct vl_compositor *c)
       return false;
    }
 
-   c->fs_palette = create_frag_shader_palette(c);
-   if (!c->fs_palette) {
-      debug_printf("Unable to create Palette-to-RGB fragment shader.\n");
+   c->fs_palette.yuv = create_frag_shader_palette(c, true);
+   if (!c->fs_palette.yuv) {
+      debug_printf("Unable to create YUV-Palette-to-RGB fragment shader.\n");
+      return false;
+   }
+
+   c->fs_palette.rgb = create_frag_shader_palette(c, false);
+   if (!c->fs_palette.rgb) {
+      debug_printf("Unable to create RGB-Palette-to-RGB fragment shader.\n");
       return false;
    }
 
@@ -221,7 +234,8 @@ static void cleanup_shaders(struct vl_compositor *c)
 
    c->pipe->delete_vs_state(c->pipe, c->vs);
    c->pipe->delete_fs_state(c->pipe, c->fs_video_buffer);
-   c->pipe->delete_fs_state(c->pipe, c->fs_palette);
+   c->pipe->delete_fs_state(c->pipe, c->fs_palette.yuv);
+   c->pipe->delete_fs_state(c->pipe, c->fs_palette.rgb);
    c->pipe->delete_fs_state(c->pipe, c->fs_rgba);
 }
 
@@ -263,6 +277,13 @@ init_pipe_state(struct vl_compositor *c)
 
    memset(&blend, 0, sizeof blend);
    blend.independent_blend_enable = 0;
+   blend.rt[0].blend_enable = 0;
+   blend.logicop_enable = 0;
+   blend.logicop_func = PIPE_LOGICOP_CLEAR;
+   blend.rt[0].colormask = PIPE_MASK_RGBA;
+   blend.dither = 0;
+   c->blend_clear = c->pipe->create_blend_state(c->pipe, &blend);
+
    blend.rt[0].blend_enable = 1;
    blend.rt[0].rgb_func = PIPE_BLEND_ADD;
    blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
@@ -270,11 +291,7 @@ init_pipe_state(struct vl_compositor *c)
    blend.rt[0].alpha_func = PIPE_BLEND_ADD;
    blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
    blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
-   blend.logicop_enable = 0;
-   blend.logicop_func = PIPE_LOGICOP_CLEAR;
-   blend.rt[0].colormask = PIPE_MASK_RGBA;
-   blend.dither = 0;
-   c->blend = c->pipe->create_blend_state(c->pipe, &blend);
+   c->blend_add = c->pipe->create_blend_state(c->pipe, &blend);
 
    memset(&rast, 0, sizeof rast);
    rast.flatshade = 1;
@@ -309,6 +326,7 @@ init_pipe_state(struct vl_compositor *c)
    dsa.alpha.ref_value = 0;
    c->dsa = c->pipe->create_depth_stencil_alpha_state(c->pipe, &dsa);
    c->pipe->bind_depth_stencil_alpha_state(c->pipe, c->dsa);
+
    return true;
 }
 
@@ -323,7 +341,8 @@ static void cleanup_pipe_state(struct vl_compositor *c)
    c->pipe->delete_depth_stencil_alpha_state(c->pipe, c->dsa);
    c->pipe->delete_sampler_state(c->pipe, c->sampler_linear);
    c->pipe->delete_sampler_state(c->pipe, c->sampler_nearest);
-   c->pipe->delete_blend_state(c->pipe, c->blend);
+   c->pipe->delete_blend_state(c->pipe, c->blend_clear);
+   c->pipe->delete_blend_state(c->pipe, c->blend_add);
    c->pipe->delete_rasterizer_state(c->pipe, c->rast);
 }
 
@@ -340,6 +359,7 @@ create_vertex_buffer(struct vl_compositor *c)
       PIPE_USAGE_STREAM,
       sizeof(struct vertex4f) * VL_COMPOSITOR_MAX_LAYERS * 4
    );
+
    return c->vertex_buf.buffer != NULL;
 }
 
@@ -453,8 +473,27 @@ gen_rect_verts(struct vertex4f *vb, struct vl_compositor_layer *layer)
    vb[3].w = layer->src.br.y;
 }
 
+static INLINE struct u_rect
+calc_drawn_area(struct vl_compositor *c, struct vl_compositor_layer *layer)
+{
+   struct u_rect result;
+
+   // scale
+   result.x0 = layer->dst.tl.x * c->viewport.scale[0] + c->viewport.translate[0];
+   result.y0 = layer->dst.tl.y * c->viewport.scale[1] + c->viewport.translate[1];
+   result.x1 = layer->dst.br.x * c->viewport.scale[0] + c->viewport.translate[0];
+   result.y1 = layer->dst.br.y * c->viewport.scale[1] + c->viewport.translate[1];
+
+   // and clip
+   result.x0 = MAX2(result.x0, c->scissor.minx);
+   result.y0 = MAX2(result.y0, c->scissor.miny);
+   result.x1 = MIN2(result.x1, c->scissor.maxx);
+   result.y1 = MIN2(result.y1, c->scissor.maxy);
+   return result;
+}
+
 static void
-gen_vertex_data(struct vl_compositor *c)
+gen_vertex_data(struct vl_compositor *c, struct u_rect *dirty)
 {
    struct vertex4f *vb;
    struct pipe_transfer *buf_transfer;
@@ -463,14 +502,14 @@ gen_vertex_data(struct vl_compositor *c)
    assert(c);
 
    vb = pipe_buffer_map(c->pipe, c->vertex_buf.buffer,
-                        PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD | PIPE_TRANSFER_DONTBLOCK,
+                        PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE | PIPE_TRANSFER_DONTBLOCK,
                         &buf_transfer);
 
    if (!vb) {
       // If buffer is still locked from last draw create a new one
       create_vertex_buffer(c);
       vb = pipe_buffer_map(c->pipe, c->vertex_buf.buffer,
-                           PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
+                           PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE,
                            &buf_transfer);
    }
 
@@ -480,15 +519,18 @@ gen_vertex_data(struct vl_compositor *c)
          gen_rect_verts(vb, layer);
          vb += 4;
 
-         if (layer->clearing &&
-             c->dirty_tl.x >= layer->dst.tl.x &&
-             c->dirty_tl.y >= layer->dst.tl.y &&
-             c->dirty_br.x <= layer->dst.br.x &&
-             c->dirty_br.y <= layer->dst.br.y) {
-
-            // We clear the dirty area anyway, no need for clear_render_target
-            c->dirty_tl.x = c->dirty_tl.y = 1.0f;
-            c->dirty_br.x = c->dirty_br.y = 0.0f;
+         if (dirty && layer->clearing) {
+            struct u_rect drawn = calc_drawn_area(c, layer);
+            if (
+             dirty->x0 >= drawn.x0 &&
+             dirty->y0 >= drawn.y0 &&
+             dirty->x1 <= drawn.x1 &&
+             dirty->y1 <= drawn.y1) {
+
+               // We clear the dirty area anyway, no need for clear_render_target
+               dirty->x0 = dirty->y0 = MAX_DIRTY;
+               dirty->x1 = dirty->y1 = MIN_DIRTY;
+            }
          }
       }
    }
@@ -497,7 +539,7 @@ gen_vertex_data(struct vl_compositor *c)
 }
 
 static void
-draw_layers(struct vl_compositor *c)
+draw_layers(struct vl_compositor *c, struct u_rect *dirty)
 {
    unsigned vb_index, i;
 
@@ -509,39 +551,49 @@ draw_layers(struct vl_compositor *c)
          struct pipe_sampler_view **samplers = &layer->sampler_views[0];
          unsigned num_sampler_views = !samplers[1] ? 1 : !samplers[2] ? 2 : 3;
 
+         c->pipe->bind_blend_state(c->pipe, layer->blend);
          c->pipe->bind_fs_state(c->pipe, layer->fs);
          c->pipe->bind_fragment_sampler_states(c->pipe, num_sampler_views, layer->samplers);
          c->pipe->set_fragment_sampler_views(c->pipe, num_sampler_views, samplers);
          util_draw_arrays(c->pipe, PIPE_PRIM_QUADS, vb_index * 4, 4);
          vb_index++;
 
-         // Remember the currently drawn area as dirty for the next draw command
-         c->dirty_tl.x = MIN2(layer->dst.tl.x, c->dirty_tl.x);
-         c->dirty_tl.y = MIN2(layer->dst.tl.y, c->dirty_tl.y);
-         c->dirty_br.x = MAX2(layer->dst.br.x, c->dirty_br.x);
-         c->dirty_br.y = MAX2(layer->dst.br.y, c->dirty_br.y);
+         if (dirty) {
+            // Remember the currently drawn area as dirty for the next draw command
+            struct u_rect drawn = calc_drawn_area(c, layer);
+            dirty->x0 = MIN2(drawn.x0, dirty->x0);
+            dirty->y0 = MIN2(drawn.y0, dirty->y0);
+            dirty->x1 = MAX2(drawn.x1, dirty->x1);
+            dirty->y1 = MAX2(drawn.y1, dirty->y1);
+         }
       }
    }
 }
 
 void
-vl_compositor_reset_dirty_area(struct vl_compositor *c)
+vl_compositor_reset_dirty_area(struct u_rect *dirty)
 {
-   assert(c);
+   assert(dirty);
 
-   c->dirty_tl.x = c->dirty_tl.y = 0.0f;
-   c->dirty_br.x = c->dirty_br.y = 1.0f;
+   dirty->x0 = dirty->y0 = MIN_DIRTY;
+   dirty->x1 = dirty->y1 = MAX_DIRTY;
 }
 
 void
-vl_compositor_set_clear_color(struct vl_compositor *c, float color[4])
+vl_compositor_set_clear_color(struct vl_compositor *c, union pipe_color_union *color)
 {
-   unsigned i;
+   assert(c);
 
+   c->clear_color = *color;
+}
+
+void
+vl_compositor_get_clear_color(struct vl_compositor *c, union pipe_color_union *color)
+{
    assert(c);
+   assert(color);
 
-   for (i = 0; i < 4; ++i)
-      c->clear_color[i] = color[i];
+   *color = c->clear_color;
 }
 
 void
@@ -553,6 +605,8 @@ vl_compositor_clear_layers(struct vl_compositor *c)
 
    c->used_layers = 0;
    for ( i = 0; i < VL_COMPOSITOR_MAX_LAYERS; ++i) {
+      c->layers[i].clearing = i ? false : true;
+      c->layers[i].blend = i ? c->blend_add : c->blend_clear;
       c->layers[i].fs = NULL;
       for ( j = 0; j < 3; j++)
          pipe_sampler_view_reference(&c->layers[i].sampler_views[j], NULL);
@@ -581,15 +635,28 @@ vl_compositor_set_csc_matrix(struct vl_compositor *c, const float matrix[16])
    memcpy
    (
       pipe_buffer_map(c->pipe, c->csc_matrix,
-                      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
+                      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE,
                       &buf_transfer),
-               matrix,
-               sizeof(csc_matrix)
+      matrix,
+      sizeof(csc_matrix)
    );
 
    pipe_buffer_unmap(c->pipe, buf_transfer);
 }
 
+void
+vl_compositor_set_layer_blend(struct vl_compositor *c,
+                              unsigned layer, void *blend,
+                              bool is_clearing)
+{
+   assert(c && blend);
+
+   assert(layer < VL_COMPOSITOR_MAX_LAYERS);
+
+   c->layers[layer].clearing = is_clearing;
+   c->layers[layer].blend = blend;
+}
+
 void
 vl_compositor_set_buffer_layer(struct vl_compositor *c,
                                unsigned layer,
@@ -605,7 +672,6 @@ vl_compositor_set_buffer_layer(struct vl_compositor *c,
    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
 
    c->used_layers |= 1 << layer;
-   c->layers[layer].clearing = true;
    c->layers[layer].fs = c->fs_video_buffer;
 
    sampler_views = buffer->get_sampler_view_components(buffer);
@@ -625,15 +691,18 @@ vl_compositor_set_palette_layer(struct vl_compositor *c,
                                 struct pipe_sampler_view *indexes,
                                 struct pipe_sampler_view *palette,
                                 struct pipe_video_rect *src_rect,
-                                struct pipe_video_rect *dst_rect)
+                                struct pipe_video_rect *dst_rect,
+                                bool include_color_conversion)
 {
    assert(c && indexes && palette);
 
    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
 
    c->used_layers |= 1 << layer;
-   c->layers[layer].clearing = false;
-   c->layers[layer].fs = c->fs_palette;
+
+   c->layers[layer].fs = include_color_conversion ?
+      c->fs_palette.yuv : c->fs_palette.rgb;
+
    c->layers[layer].samplers[0] = c->sampler_linear;
    c->layers[layer].samplers[1] = c->sampler_nearest;
    c->layers[layer].samplers[2] = NULL;
@@ -643,7 +712,6 @@ vl_compositor_set_palette_layer(struct vl_compositor *c,
    calc_src_and_dst(&c->layers[layer], indexes->texture->width0, indexes->texture->height0,
                     src_rect ? *src_rect : default_rect(&c->layers[layer]),
                     dst_rect ? *dst_rect : default_rect(&c->layers[layer]));
-
 }
 
 void
@@ -658,7 +726,6 @@ vl_compositor_set_rgba_layer(struct vl_compositor *c,
    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
 
    c->used_layers |= 1 << layer;
-   c->layers[layer].clearing = rgba->swizzle_a == PIPE_SWIZZLE_ONE;
    c->layers[layer].fs = c->fs_rgba;
    c->layers[layer].samplers[0] = c->sampler_linear;
    c->layers[layer].samplers[1] = NULL;
@@ -672,14 +739,12 @@ vl_compositor_set_rgba_layer(struct vl_compositor *c,
 }
 
 void
-vl_compositor_render(struct vl_compositor *c,
-                     enum pipe_mpeg12_picture_type picture_type,
-                     struct pipe_surface           *dst_surface,
-                     struct pipe_video_rect        *dst_area,
-                     struct pipe_video_rect        *dst_clip)
+vl_compositor_render(struct vl_compositor   *c,
+                     struct pipe_surface    *dst_surface,
+                     struct pipe_video_rect *dst_area,
+                     struct pipe_video_rect *dst_clip,
+                     struct u_rect          *dirty_area)
 {
-   struct pipe_scissor_state scissor;
-
    assert(c);
    assert(dst_surface);
 
@@ -700,36 +765,38 @@ vl_compositor_render(struct vl_compositor *c,
    }
 
    if (dst_clip) {
-      scissor.minx = dst_clip->x;
-      scissor.miny = dst_clip->y;
-      scissor.maxx = dst_clip->x + dst_clip->w;
-      scissor.maxy = dst_clip->y + dst_clip->h;
+      c->scissor.minx = dst_clip->x;
+      c->scissor.miny = dst_clip->y;
+      c->scissor.maxx = dst_clip->x + dst_clip->w;
+      c->scissor.maxy = dst_clip->y + dst_clip->h;
    } else {
-      scissor.minx = 0;
-      scissor.miny = 0;
-      scissor.maxx = dst_surface->width;
-      scissor.maxy = dst_surface->height;
+      c->scissor.minx = 0;
+      c->scissor.miny = 0;
+      c->scissor.maxx = dst_surface->width;
+      c->scissor.maxy = dst_surface->height;
    }
 
-   gen_vertex_data(c);
+   gen_vertex_data(c, dirty_area);
+
+   if (dirty_area && (dirty_area->x0 < dirty_area->x1 ||
+                      dirty_area->y0 < dirty_area->y1)) {
 
-   if (c->dirty_tl.x < c->dirty_br.x || c->dirty_tl.y < c->dirty_br.y) {
-      util_clear_render_target(c->pipe, dst_surface, c->clear_color, 0, 0, dst_surface->width, dst_surface->height);
-      c->dirty_tl.x = c->dirty_tl.y = 1.0f;
-      c->dirty_br.x = c->dirty_br.y = 0.0f;
+      c->pipe->clear_render_target(c->pipe, dst_surface, &c->clear_color,
+                                   0, 0, dst_surface->width, dst_surface->height);
+      dirty_area->x0 = dirty_area->y0 = MAX_DIRTY;
+      dirty_area->x0 = dirty_area->y1 = MIN_DIRTY;
    }
 
-   c->pipe->set_scissor_state(c->pipe, &scissor);
+   c->pipe->set_scissor_state(c->pipe, &c->scissor);
    c->pipe->set_framebuffer_state(c->pipe, &c->fb_state);
    c->pipe->set_viewport_state(c->pipe, &c->viewport);
    c->pipe->bind_vs_state(c->pipe, c->vs);
    c->pipe->set_vertex_buffers(c->pipe, 1, &c->vertex_buf);
    c->pipe->bind_vertex_elements_state(c->pipe, c->vertex_elems_state);
    c->pipe->set_constant_buffer(c->pipe, PIPE_SHADER_FRAGMENT, 0, c->csc_matrix);
-   c->pipe->bind_blend_state(c->pipe, c->blend);
    c->pipe->bind_rasterizer_state(c->pipe, c->rast);
 
-   draw_layers(c);
+   draw_layers(c, dirty_area);
 }
 
 bool
@@ -746,6 +813,7 @@ vl_compositor_init(struct vl_compositor *c, struct pipe_context *pipe)
       cleanup_pipe_state(c);
       return false;
    }
+
    if (!init_buffers(c)) {
       cleanup_shaders(c);
       cleanup_pipe_state(c);
@@ -757,9 +825,8 @@ vl_compositor_init(struct vl_compositor *c, struct pipe_context *pipe)
    vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, csc_matrix);
    vl_compositor_set_csc_matrix(c, csc_matrix);
 
-   c->clear_color[0] = c->clear_color[1] = 0.0f;
-   c->clear_color[2] = c->clear_color[3] = 0.0f;
-   vl_compositor_reset_dirty_area(c);
+   c->clear_color.f[0] = c->clear_color.f[1] = 0.0f;
+   c->clear_color.f[2] = c->clear_color.f[3] = 0.0f;
 
    return true;
 }