Path: blob/21.2-virgl/src/gallium/frontends/xa/xa_yuv.c
4561 views
/**********************************************************1* Copyright 2009-2011 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23*********************************************************24* Authors:25* Zack Rusin <zackr-at-vmware-dot-com>26* Thomas Hellstrom <thellstrom-at-vmware-dot-com>27*/2829#include "xa_context.h"30#include "xa_priv.h"31#include "util/u_inlines.h"32#include "util/u_sampler.h"33#include "util/u_surface.h"34#include "cso_cache/cso_context.h"3536static void37xa_yuv_bind_blend_state(struct xa_context *r)38{39struct pipe_blend_state blend;4041memset(&blend, 0, sizeof(struct pipe_blend_state));42blend.rt[0].blend_enable = 0;43blend.rt[0].colormask = PIPE_MASK_RGBA;4445/* porter&duff src */46blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;47blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;48blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;49blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;5051cso_set_blend(r->cso, &blend);52}5354static void55xa_yuv_bind_shaders(struct xa_context *r)56{57unsigned vs_traits = 0, fs_traits = 0;58struct xa_shader shader;5960vs_traits |= VS_YUV;61fs_traits |= FS_YUV;6263shader = xa_shaders_get(r->shaders, vs_traits, fs_traits);64cso_set_vertex_shader_handle(r->cso, shader.vs);65cso_set_fragment_shader_handle(r->cso, shader.fs);66}6768static void69xa_yuv_bind_samplers(struct xa_context *r, struct xa_surface *yuv[])70{71struct pipe_sampler_state *samplers[3];72struct pipe_sampler_state sampler;73struct pipe_sampler_view view_templ;74unsigned int i;7576memset(&sampler, 0, sizeof(struct pipe_sampler_state));7778sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;79sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;80sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;81sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;82sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;83sampler.normalized_coords = 1;8485for (i = 0; i < 3; ++i) {86samplers[i] = &sampler;87u_sampler_view_default_template(&view_templ, yuv[i]->tex,88yuv[i]->tex->format);8990r->bound_sampler_views[i] =91r->pipe->create_sampler_view(r->pipe, yuv[i]->tex, &view_templ);92}93r->num_bound_samplers = 3;94cso_set_samplers(r->cso, PIPE_SHADER_FRAGMENT, 3, (const struct pipe_sampler_state **)samplers);95r->pipe->set_sampler_views(r->pipe, PIPE_SHADER_FRAGMENT, 0, 3, 0, r->bound_sampler_views);96}9798static void99xa_yuv_fs_constants(struct xa_context *r, const float conversion_matrix[])100{101const int param_bytes = 16 * sizeof(float);102103renderer_set_constants(r, PIPE_SHADER_FRAGMENT,104conversion_matrix, param_bytes);105}106107XA_EXPORT int108xa_yuv_planar_blit(struct xa_context *r,109int src_x,110int src_y,111int src_w,112int src_h,113int dst_x,114int dst_y,115int dst_w,116int dst_h,117struct xa_box *box,118unsigned int num_boxes,119const float conversion_matrix[],120struct xa_surface *dst, struct xa_surface *yuv[])121{122float scale_x;123float scale_y;124int ret;125126if (dst_w == 0 || dst_h == 0)127return XA_ERR_NONE;128129ret = xa_ctx_srf_create(r, dst);130if (ret != XA_ERR_NONE)131return -XA_ERR_NORES;132133renderer_bind_destination(r, r->srf);134xa_yuv_bind_blend_state(r);135xa_yuv_bind_shaders(r);136xa_yuv_bind_samplers(r, yuv);137xa_yuv_fs_constants(r, conversion_matrix);138139scale_x = (float)src_w / (float)dst_w;140scale_y = (float)src_h / (float)dst_h;141142while (num_boxes--) {143int x = box->x1;144int y = box->y1;145int w = box->x2 - box->x1;146int h = box->y2 - box->y1;147148xa_scissor_update(r, x, y, box->x2, box->y2);149renderer_draw_yuv(r,150(float)src_x + scale_x * (x - dst_x),151(float)src_y + scale_y * (y - dst_y),152scale_x * w, scale_y * h, x, y, w, h, yuv);153box++;154}155156xa_context_flush(r);157158xa_ctx_sampler_views_destroy(r);159xa_ctx_srf_destroy(r);160161return XA_ERR_NONE;162}163164165