Path: blob/21.2-virgl/src/gallium/auxiliary/vl/vl_compositor.c
4565 views
/**************************************************************************1*2* Copyright 2009 Younes Manton.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "util/u_sampler.h"2829#include "vl_compositor_gfx.h"30#include "vl_compositor_cs.h"3132static bool33init_shaders(struct vl_compositor *c)34{35assert(c);3637if (c->pipe_cs_composit_supported) {38if (!vl_compositor_cs_init_shaders(c))39return false;4041} else if (c->pipe_gfx_supported) {42c->fs_video_buffer = create_frag_shader_video_buffer(c);43if (!c->fs_video_buffer) {44debug_printf("Unable to create YCbCr-to-RGB fragment shader.\n");45return false;46}4748c->fs_weave_rgb = create_frag_shader_weave_rgb(c);49if (!c->fs_weave_rgb) {50debug_printf("Unable to create YCbCr-to-RGB weave fragment shader.\n");51return false;52}5354c->fs_yuv.weave.y = create_frag_shader_deint_yuv(c, true, true);55c->fs_yuv.weave.uv = create_frag_shader_deint_yuv(c, false, true);56c->fs_yuv.bob.y = create_frag_shader_deint_yuv(c, true, false);57c->fs_yuv.bob.uv = create_frag_shader_deint_yuv(c, false, false);58if (!c->fs_yuv.weave.y || !c->fs_yuv.weave.uv ||59!c->fs_yuv.bob.y || !c->fs_yuv.bob.uv) {60debug_printf("Unable to create YCbCr i-to-YCbCr p deint fragment shader.\n");61return false;62}63}6465if (c->pipe_gfx_supported) {66c->vs = create_vert_shader(c);67if (!c->vs) {68debug_printf("Unable to create vertex shader.\n");69return false;70}7172c->fs_palette.yuv = create_frag_shader_palette(c, true);73if (!c->fs_palette.yuv) {74debug_printf("Unable to create YUV-Palette-to-RGB fragment shader.\n");75return false;76}7778c->fs_palette.rgb = create_frag_shader_palette(c, false);79if (!c->fs_palette.rgb) {80debug_printf("Unable to create RGB-Palette-to-RGB fragment shader.\n");81return false;82}8384c->fs_rgb_yuv.y = create_frag_shader_rgb_yuv(c, true);85c->fs_rgb_yuv.uv = create_frag_shader_rgb_yuv(c, false);86if (!c->fs_rgb_yuv.y || !c->fs_rgb_yuv.uv) {87debug_printf("Unable to create RGB-to-YUV fragment shader.\n");88return false;89}9091c->fs_rgba = create_frag_shader_rgba(c);92if (!c->fs_rgba) {93debug_printf("Unable to create RGB-to-RGB fragment shader.\n");94return false;95}96}9798return true;99}100101static void cleanup_shaders(struct vl_compositor *c)102{103assert(c);104105if (c->pipe_cs_composit_supported) {106vl_compositor_cs_cleanup_shaders(c);107} else if (c->pipe_gfx_supported) {108c->pipe->delete_fs_state(c->pipe, c->fs_video_buffer);109c->pipe->delete_fs_state(c->pipe, c->fs_weave_rgb);110c->pipe->delete_fs_state(c->pipe, c->fs_yuv.weave.y);111c->pipe->delete_fs_state(c->pipe, c->fs_yuv.weave.uv);112c->pipe->delete_fs_state(c->pipe, c->fs_yuv.bob.y);113c->pipe->delete_fs_state(c->pipe, c->fs_yuv.bob.uv);114}115116if (c->pipe_gfx_supported) {117c->pipe->delete_vs_state(c->pipe, c->vs);118c->pipe->delete_fs_state(c->pipe, c->fs_palette.yuv);119c->pipe->delete_fs_state(c->pipe, c->fs_palette.rgb);120c->pipe->delete_fs_state(c->pipe, c->fs_rgb_yuv.y);121c->pipe->delete_fs_state(c->pipe, c->fs_rgb_yuv.uv);122c->pipe->delete_fs_state(c->pipe, c->fs_rgba);123}124}125126static bool127init_pipe_state(struct vl_compositor *c)128{129struct pipe_rasterizer_state rast;130struct pipe_sampler_state sampler;131struct pipe_blend_state blend;132struct pipe_depth_stencil_alpha_state dsa;133unsigned i;134135assert(c);136137c->fb_state.nr_cbufs = 1;138c->fb_state.zsbuf = NULL;139140memset(&sampler, 0, sizeof(sampler));141sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;142sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;143sampler.wrap_r = PIPE_TEX_WRAP_REPEAT;144sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;145sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;146sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;147sampler.compare_mode = PIPE_TEX_COMPARE_NONE;148sampler.compare_func = PIPE_FUNC_ALWAYS;149sampler.normalized_coords = 1;150151c->sampler_linear = c->pipe->create_sampler_state(c->pipe, &sampler);152153sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;154sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;155c->sampler_nearest = c->pipe->create_sampler_state(c->pipe, &sampler);156157if (c->pipe_gfx_supported) {158memset(&blend, 0, sizeof blend);159blend.independent_blend_enable = 0;160blend.rt[0].blend_enable = 0;161blend.logicop_enable = 0;162blend.logicop_func = PIPE_LOGICOP_CLEAR;163blend.rt[0].colormask = PIPE_MASK_RGBA;164blend.dither = 0;165c->blend_clear = c->pipe->create_blend_state(c->pipe, &blend);166167blend.rt[0].blend_enable = 1;168blend.rt[0].rgb_func = PIPE_BLEND_ADD;169blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;170blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;171blend.rt[0].alpha_func = PIPE_BLEND_ADD;172blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;173blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;174c->blend_add = c->pipe->create_blend_state(c->pipe, &blend);175176memset(&rast, 0, sizeof rast);177rast.flatshade = 0;178rast.front_ccw = 1;179rast.cull_face = PIPE_FACE_NONE;180rast.fill_back = PIPE_POLYGON_MODE_FILL;181rast.fill_front = PIPE_POLYGON_MODE_FILL;182rast.scissor = 1;183rast.line_width = 1;184rast.point_size_per_vertex = 1;185rast.offset_units = 1;186rast.offset_scale = 1;187rast.half_pixel_center = 1;188rast.bottom_edge_rule = 1;189rast.depth_clip_near = 1;190rast.depth_clip_far = 1;191192c->rast = c->pipe->create_rasterizer_state(c->pipe, &rast);193194memset(&dsa, 0, sizeof dsa);195dsa.depth_enabled = 0;196dsa.depth_writemask = 0;197dsa.depth_func = PIPE_FUNC_ALWAYS;198for (i = 0; i < 2; ++i) {199dsa.stencil[i].enabled = 0;200dsa.stencil[i].func = PIPE_FUNC_ALWAYS;201dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;202dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;203dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;204dsa.stencil[i].valuemask = 0;205dsa.stencil[i].writemask = 0;206}207dsa.alpha_enabled = 0;208dsa.alpha_func = PIPE_FUNC_ALWAYS;209dsa.alpha_ref_value = 0;210c->dsa = c->pipe->create_depth_stencil_alpha_state(c->pipe, &dsa);211c->pipe->bind_depth_stencil_alpha_state(c->pipe, c->dsa);212}213214return true;215}216217static void cleanup_pipe_state(struct vl_compositor *c)218{219assert(c);220221if (c->pipe_gfx_supported) {222/* Asserted in softpipe_delete_fs_state() for some reason */223c->pipe->bind_vs_state(c->pipe, NULL);224c->pipe->bind_fs_state(c->pipe, NULL);225226c->pipe->delete_depth_stencil_alpha_state(c->pipe, c->dsa);227c->pipe->delete_blend_state(c->pipe, c->blend_clear);228c->pipe->delete_blend_state(c->pipe, c->blend_add);229c->pipe->delete_rasterizer_state(c->pipe, c->rast);230}231c->pipe->delete_sampler_state(c->pipe, c->sampler_linear);232c->pipe->delete_sampler_state(c->pipe, c->sampler_nearest);233}234235static bool236init_buffers(struct vl_compositor *c)237{238struct pipe_vertex_element vertex_elems[3];239240assert(c);241242/*243* Create our vertex buffer and vertex buffer elements244*/245c->vertex_buf.stride = sizeof(struct vertex2f) + sizeof(struct vertex4f) * 2;246c->vertex_buf.buffer_offset = 0;247c->vertex_buf.buffer.resource = NULL;248c->vertex_buf.is_user_buffer = false;249250if (c->pipe_gfx_supported) {251vertex_elems[0].src_offset = 0;252vertex_elems[0].instance_divisor = 0;253vertex_elems[0].vertex_buffer_index = 0;254vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT;255vertex_elems[1].src_offset = sizeof(struct vertex2f);256vertex_elems[1].instance_divisor = 0;257vertex_elems[1].vertex_buffer_index = 0;258vertex_elems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;259vertex_elems[2].src_offset = sizeof(struct vertex2f) + sizeof(struct vertex4f);260vertex_elems[2].instance_divisor = 0;261vertex_elems[2].vertex_buffer_index = 0;262vertex_elems[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;263c->vertex_elems_state = c->pipe->create_vertex_elements_state(c->pipe, 3, vertex_elems);264}265266return true;267}268269static void270cleanup_buffers(struct vl_compositor *c)271{272assert(c);273274if (c->pipe_gfx_supported) {275c->pipe->delete_vertex_elements_state(c->pipe, c->vertex_elems_state);276}277pipe_resource_reference(&c->vertex_buf.buffer.resource, NULL);278}279280static inline struct u_rect281default_rect(struct vl_compositor_layer *layer)282{283struct pipe_resource *res = layer->sampler_views[0]->texture;284struct u_rect rect = { 0, res->width0, 0, res->height0 * res->array_size };285return rect;286}287288static inline struct vertex2f289calc_topleft(struct vertex2f size, struct u_rect rect)290{291struct vertex2f res = { rect.x0 / size.x, rect.y0 / size.y };292return res;293}294295static inline struct vertex2f296calc_bottomright(struct vertex2f size, struct u_rect rect)297{298struct vertex2f res = { rect.x1 / size.x, rect.y1 / size.y };299return res;300}301302static inline void303calc_src_and_dst(struct vl_compositor_layer *layer, unsigned width, unsigned height,304struct u_rect src, struct u_rect dst)305{306struct vertex2f size = { width, height };307308layer->src.tl = calc_topleft(size, src);309layer->src.br = calc_bottomright(size, src);310layer->dst.tl = calc_topleft(size, dst);311layer->dst.br = calc_bottomright(size, dst);312layer->zw.x = 0.0f;313layer->zw.y = size.y;314}315316static void317set_yuv_layer(struct vl_compositor_state *s, struct vl_compositor *c,318unsigned layer, struct pipe_video_buffer *buffer,319struct u_rect *src_rect, struct u_rect *dst_rect,320bool y, enum vl_compositor_deinterlace deinterlace)321{322struct pipe_sampler_view **sampler_views;323float half_a_line;324unsigned i;325326assert(s && c && buffer);327328assert(layer < VL_COMPOSITOR_MAX_LAYERS);329330s->interlaced = buffer->interlaced;331s->used_layers |= 1 << layer;332sampler_views = buffer->get_sampler_view_components(buffer);333for (i = 0; i < 3; ++i) {334s->layers[layer].samplers[i] = c->sampler_linear;335pipe_sampler_view_reference(&s->layers[layer].sampler_views[i], sampler_views[i]);336}337338calc_src_and_dst(&s->layers[layer], buffer->width, buffer->height,339src_rect ? *src_rect : default_rect(&s->layers[layer]),340dst_rect ? *dst_rect : default_rect(&s->layers[layer]));341342half_a_line = 0.5f / s->layers[layer].zw.y;343344switch(deinterlace) {345case VL_COMPOSITOR_BOB_TOP:346s->layers[layer].zw.x = 0.0f;347s->layers[layer].src.tl.y += half_a_line;348s->layers[layer].src.br.y += half_a_line;349if (c->pipe_gfx_supported)350s->layers[layer].fs = (y) ? c->fs_yuv.bob.y : c->fs_yuv.bob.uv;351if (c->pipe_cs_composit_supported)352s->layers[layer].cs = (y) ? c->cs_yuv.bob.y : c->cs_yuv.bob.uv;353break;354355case VL_COMPOSITOR_BOB_BOTTOM:356s->layers[layer].zw.x = 1.0f;357s->layers[layer].src.tl.y -= half_a_line;358s->layers[layer].src.br.y -= half_a_line;359if (c->pipe_gfx_supported)360s->layers[layer].fs = (y) ? c->fs_yuv.bob.y : c->fs_yuv.bob.uv;361if (c->pipe_cs_composit_supported)362s->layers[layer].cs = (y) ? c->cs_yuv.bob.y : c->cs_yuv.bob.uv;363break;364365default:366if (c->pipe_gfx_supported)367s->layers[layer].fs = (y) ? c->fs_yuv.weave.y : c->fs_yuv.weave.uv;368if (c->pipe_cs_composit_supported)369s->layers[layer].cs = (y) ? c->cs_yuv.weave.y : c->cs_yuv.weave.uv;370break;371}372}373374static void375set_rgb_to_yuv_layer(struct vl_compositor_state *s, struct vl_compositor *c,376unsigned layer, struct pipe_sampler_view *v,377struct u_rect *src_rect, struct u_rect *dst_rect, bool y)378{379vl_csc_matrix csc_matrix;380381assert(s && c && v);382383assert(layer < VL_COMPOSITOR_MAX_LAYERS);384385s->used_layers |= 1 << layer;386387s->layers[layer].fs = y? c->fs_rgb_yuv.y : c->fs_rgb_yuv.uv;388389vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_709_REV, NULL, false, &csc_matrix);390vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix, 1.0f, 0.0f);391392s->layers[layer].samplers[0] = c->sampler_linear;393s->layers[layer].samplers[1] = NULL;394s->layers[layer].samplers[2] = NULL;395396pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], v);397pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], NULL);398pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);399400calc_src_and_dst(&s->layers[layer], v->texture->width0, v->texture->height0,401src_rect ? *src_rect : default_rect(&s->layers[layer]),402dst_rect ? *dst_rect : default_rect(&s->layers[layer]));403}404405void406vl_compositor_reset_dirty_area(struct u_rect *dirty)407{408assert(dirty);409410dirty->x0 = dirty->y0 = VL_COMPOSITOR_MIN_DIRTY;411dirty->x1 = dirty->y1 = VL_COMPOSITOR_MAX_DIRTY;412}413414void415vl_compositor_set_clear_color(struct vl_compositor_state *s, union pipe_color_union *color)416{417assert(s);418assert(color);419420s->clear_color = *color;421}422423void424vl_compositor_get_clear_color(struct vl_compositor_state *s, union pipe_color_union *color)425{426assert(s);427assert(color);428429*color = s->clear_color;430}431432void433vl_compositor_clear_layers(struct vl_compositor_state *s)434{435unsigned i, j;436437assert(s);438s->interlaced = false;439s->used_layers = 0;440for ( i = 0; i < VL_COMPOSITOR_MAX_LAYERS; ++i) {441struct vertex4f v_one = { 1.0f, 1.0f, 1.0f, 1.0f };442s->layers[i].clearing = i ? false : true;443s->layers[i].blend = NULL;444s->layers[i].fs = NULL;445s->layers[i].cs = NULL;446s->layers[i].viewport.scale[2] = 1;447s->layers[i].viewport.translate[2] = 0;448s->layers[i].viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;449s->layers[i].viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;450s->layers[i].viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;451s->layers[i].viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;452s->layers[i].rotate = VL_COMPOSITOR_ROTATE_0;453454for ( j = 0; j < 3; j++)455pipe_sampler_view_reference(&s->layers[i].sampler_views[j], NULL);456for ( j = 0; j < 4; ++j)457s->layers[i].colors[j] = v_one;458}459}460461void462vl_compositor_cleanup(struct vl_compositor *c)463{464assert(c);465466cleanup_buffers(c);467cleanup_shaders(c);468cleanup_pipe_state(c);469}470471bool472vl_compositor_set_csc_matrix(struct vl_compositor_state *s,473vl_csc_matrix const *matrix,474float luma_min, float luma_max)475{476struct pipe_transfer *buf_transfer;477478assert(s);479480float *ptr = pipe_buffer_map(s->pipe, s->shader_params,481PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE,482&buf_transfer);483484if (!ptr)485return false;486487memcpy(ptr, matrix, sizeof(vl_csc_matrix));488489ptr += sizeof(vl_csc_matrix)/sizeof(float);490ptr[0] = luma_min;491ptr[1] = luma_max;492493pipe_buffer_unmap(s->pipe, buf_transfer);494495return true;496}497498void499vl_compositor_set_dst_clip(struct vl_compositor_state *s, struct u_rect *dst_clip)500{501assert(s);502503s->scissor_valid = dst_clip != NULL;504if (dst_clip) {505s->scissor.minx = dst_clip->x0;506s->scissor.miny = dst_clip->y0;507s->scissor.maxx = dst_clip->x1;508s->scissor.maxy = dst_clip->y1;509}510}511512void513vl_compositor_set_layer_blend(struct vl_compositor_state *s,514unsigned layer, void *blend,515bool is_clearing)516{517assert(s && blend);518519assert(layer < VL_COMPOSITOR_MAX_LAYERS);520521s->layers[layer].clearing = is_clearing;522s->layers[layer].blend = blend;523}524525void526vl_compositor_set_layer_dst_area(struct vl_compositor_state *s,527unsigned layer, struct u_rect *dst_area)528{529assert(s);530531assert(layer < VL_COMPOSITOR_MAX_LAYERS);532533s->layers[layer].viewport_valid = dst_area != NULL;534if (dst_area) {535s->layers[layer].viewport.scale[0] = dst_area->x1 - dst_area->x0;536s->layers[layer].viewport.scale[1] = dst_area->y1 - dst_area->y0;537s->layers[layer].viewport.translate[0] = dst_area->x0;538s->layers[layer].viewport.translate[1] = dst_area->y0;539}540}541542void543vl_compositor_set_buffer_layer(struct vl_compositor_state *s,544struct vl_compositor *c,545unsigned layer,546struct pipe_video_buffer *buffer,547struct u_rect *src_rect,548struct u_rect *dst_rect,549enum vl_compositor_deinterlace deinterlace)550{551struct pipe_sampler_view **sampler_views;552unsigned i;553554assert(s && c && buffer);555556assert(layer < VL_COMPOSITOR_MAX_LAYERS);557558s->interlaced = buffer->interlaced;559s->used_layers |= 1 << layer;560sampler_views = buffer->get_sampler_view_components(buffer);561for (i = 0; i < 3; ++i) {562s->layers[layer].samplers[i] = c->sampler_linear;563pipe_sampler_view_reference(&s->layers[layer].sampler_views[i], sampler_views[i]);564}565566calc_src_and_dst(&s->layers[layer], buffer->width, buffer->height,567src_rect ? *src_rect : default_rect(&s->layers[layer]),568dst_rect ? *dst_rect : default_rect(&s->layers[layer]));569570if (buffer->interlaced) {571float half_a_line = 0.5f / s->layers[layer].zw.y;572switch(deinterlace) {573case VL_COMPOSITOR_NONE:574case VL_COMPOSITOR_MOTION_ADAPTIVE:575case VL_COMPOSITOR_WEAVE:576if (c->pipe_cs_composit_supported)577s->layers[layer].cs = c->cs_weave_rgb;578else if (c->pipe_gfx_supported)579s->layers[layer].fs = c->fs_weave_rgb;580break;581582case VL_COMPOSITOR_BOB_TOP:583s->layers[layer].zw.x = 0.0f;584s->layers[layer].src.tl.y += half_a_line;585s->layers[layer].src.br.y += half_a_line;586if (c->pipe_cs_composit_supported)587s->layers[layer].cs = c->cs_video_buffer;588else if (c->pipe_gfx_supported)589s->layers[layer].fs = c->fs_video_buffer;590break;591592case VL_COMPOSITOR_BOB_BOTTOM:593s->layers[layer].zw.x = 1.0f;594s->layers[layer].src.tl.y -= half_a_line;595s->layers[layer].src.br.y -= half_a_line;596if (c->pipe_cs_composit_supported)597s->layers[layer].cs = c->cs_video_buffer;598else if (c->pipe_gfx_supported)599s->layers[layer].fs = c->fs_video_buffer;600break;601}602603} else {604if (c->pipe_cs_composit_supported)605s->layers[layer].cs = c->cs_video_buffer;606else if (c->pipe_gfx_supported)607s->layers[layer].fs = c->fs_video_buffer;608}609}610611void612vl_compositor_set_palette_layer(struct vl_compositor_state *s,613struct vl_compositor *c,614unsigned layer,615struct pipe_sampler_view *indexes,616struct pipe_sampler_view *palette,617struct u_rect *src_rect,618struct u_rect *dst_rect,619bool include_color_conversion)620{621assert(s && c && indexes && palette);622623assert(layer < VL_COMPOSITOR_MAX_LAYERS);624625s->used_layers |= 1 << layer;626627s->layers[layer].fs = include_color_conversion ?628c->fs_palette.yuv : c->fs_palette.rgb;629630s->layers[layer].samplers[0] = c->sampler_linear;631s->layers[layer].samplers[1] = c->sampler_nearest;632s->layers[layer].samplers[2] = NULL;633pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], indexes);634pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], palette);635pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);636calc_src_and_dst(&s->layers[layer], indexes->texture->width0, indexes->texture->height0,637src_rect ? *src_rect : default_rect(&s->layers[layer]),638dst_rect ? *dst_rect : default_rect(&s->layers[layer]));639}640641void642vl_compositor_set_rgba_layer(struct vl_compositor_state *s,643struct vl_compositor *c,644unsigned layer,645struct pipe_sampler_view *rgba,646struct u_rect *src_rect,647struct u_rect *dst_rect,648struct vertex4f *colors)649{650unsigned i;651652assert(s && c && rgba);653654assert(layer < VL_COMPOSITOR_MAX_LAYERS);655656s->used_layers |= 1 << layer;657s->layers[layer].fs = c->fs_rgba;658s->layers[layer].samplers[0] = c->sampler_linear;659s->layers[layer].samplers[1] = NULL;660s->layers[layer].samplers[2] = NULL;661pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], rgba);662pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], NULL);663pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);664calc_src_and_dst(&s->layers[layer], rgba->texture->width0, rgba->texture->height0,665src_rect ? *src_rect : default_rect(&s->layers[layer]),666dst_rect ? *dst_rect : default_rect(&s->layers[layer]));667668if (colors)669for (i = 0; i < 4; ++i)670s->layers[layer].colors[i] = colors[i];671}672673void674vl_compositor_set_layer_rotation(struct vl_compositor_state *s,675unsigned layer,676enum vl_compositor_rotation rotate)677{678assert(s);679assert(layer < VL_COMPOSITOR_MAX_LAYERS);680s->layers[layer].rotate = rotate;681}682683void684vl_compositor_yuv_deint_full(struct vl_compositor_state *s,685struct vl_compositor *c,686struct pipe_video_buffer *src,687struct pipe_video_buffer *dst,688struct u_rect *src_rect,689struct u_rect *dst_rect,690enum vl_compositor_deinterlace deinterlace)691{692struct pipe_surface **dst_surfaces;693694dst_surfaces = dst->get_surfaces(dst);695vl_compositor_clear_layers(s);696697set_yuv_layer(s, c, 0, src, src_rect, NULL, true, deinterlace);698vl_compositor_set_layer_dst_area(s, 0, dst_rect);699vl_compositor_render(s, c, dst_surfaces[0], NULL, false);700701if (dst_rect) {702dst_rect->x1 /= 2;703dst_rect->y1 /= 2;704}705706set_yuv_layer(s, c, 0, src, src_rect, NULL, false, deinterlace);707vl_compositor_set_layer_dst_area(s, 0, dst_rect);708vl_compositor_render(s, c, dst_surfaces[1], NULL, false);709710s->pipe->flush(s->pipe, NULL, 0);711}712713void714vl_compositor_convert_rgb_to_yuv(struct vl_compositor_state *s,715struct vl_compositor *c,716unsigned layer,717struct pipe_resource *src_res,718struct pipe_video_buffer *dst,719struct u_rect *src_rect,720struct u_rect *dst_rect)721{722struct pipe_sampler_view *sv, sv_templ;723struct pipe_surface **dst_surfaces;724725dst_surfaces = dst->get_surfaces(dst);726727memset(&sv_templ, 0, sizeof(sv_templ));728u_sampler_view_default_template(&sv_templ, src_res, src_res->format);729sv = s->pipe->create_sampler_view(s->pipe, src_res, &sv_templ);730731vl_compositor_clear_layers(s);732733set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, true);734vl_compositor_set_layer_dst_area(s, 0, dst_rect);735vl_compositor_render(s, c, dst_surfaces[0], NULL, false);736737if (dst_rect) {738dst_rect->x1 /= 2;739dst_rect->y1 /= 2;740}741742set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, false);743vl_compositor_set_layer_dst_area(s, 0, dst_rect);744vl_compositor_render(s, c, dst_surfaces[1], NULL, false);745pipe_sampler_view_reference(&sv, NULL);746747s->pipe->flush(s->pipe, NULL, 0);748}749750void751vl_compositor_render(struct vl_compositor_state *s,752struct vl_compositor *c,753struct pipe_surface *dst_surface,754struct u_rect *dirty_area,755bool clear_dirty)756{757assert(s);758759if (s->layers->cs)760vl_compositor_cs_render(s, c, dst_surface, dirty_area, clear_dirty);761else if (s->layers->fs)762vl_compositor_gfx_render(s, c, dst_surface, dirty_area, clear_dirty);763else764debug_warning("Hardware don't support.\n");;765}766767bool768vl_compositor_init(struct vl_compositor *c, struct pipe_context *pipe)769{770assert(c);771772memset(c, 0, sizeof(*c));773774c->pipe_cs_composit_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_PREFER_COMPUTE_FOR_MULTIMEDIA) &&775pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_TEX_TXF_LZ) &&776pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_DIV);777778c->pipe_gfx_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_GRAPHICS);779c->pipe = pipe;780781c->deinterlace = VL_COMPOSITOR_NONE;782783if (!init_pipe_state(c)) {784return false;785}786787if (!init_shaders(c)) {788cleanup_pipe_state(c);789return false;790}791792if (!init_buffers(c)) {793cleanup_shaders(c);794cleanup_pipe_state(c);795return false;796}797798return true;799}800801bool802vl_compositor_init_state(struct vl_compositor_state *s, struct pipe_context *pipe)803{804vl_csc_matrix csc_matrix;805806assert(s);807808memset(s, 0, sizeof(*s));809810s->pipe = pipe;811812s->clear_color.f[0] = s->clear_color.f[1] = 0.0f;813s->clear_color.f[2] = s->clear_color.f[3] = 0.0f;814815/*816* Create our fragment shader's constant buffer817* Const buffer contains the color conversion matrix and bias vectors818*/819/* XXX: Create with IMMUTABLE/STATIC... although it does change every once in a long while... */820s->shader_params = pipe_buffer_create_const0821(822pipe->screen,823PIPE_BIND_CONSTANT_BUFFER,824PIPE_USAGE_DEFAULT,825sizeof(csc_matrix) + 6*sizeof(float) + 10*sizeof(int)826);827828if (!s->shader_params)829return false;830831vl_compositor_clear_layers(s);832833vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, &csc_matrix);834if (!vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix, 1.0f, 0.0f))835return false;836837return true;838}839840void841vl_compositor_cleanup_state(struct vl_compositor_state *s)842{843assert(s);844845vl_compositor_clear_layers(s);846pipe_resource_reference(&s->shader_params, NULL);847}848849850