17ec681f3Smrg/**********************************************************
27ec681f3Smrg * Copyright 2009-2011 VMware, Inc. All rights reserved.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person
57ec681f3Smrg * obtaining a copy of this software and associated documentation
67ec681f3Smrg * files (the "Software"), to deal in the Software without
77ec681f3Smrg * restriction, including without limitation the rights to use, copy,
87ec681f3Smrg * modify, merge, publish, distribute, sublicense, and/or sell copies
97ec681f3Smrg * of the Software, and to permit persons to whom the Software is
107ec681f3Smrg * furnished to do so, subject to the following conditions:
117ec681f3Smrg *
127ec681f3Smrg * The above copyright notice and this permission notice shall be
137ec681f3Smrg * included in all copies or substantial portions of the Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
167ec681f3Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
177ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
187ec681f3Smrg * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
197ec681f3Smrg * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
207ec681f3Smrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
217ec681f3Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
227ec681f3Smrg * SOFTWARE.
237ec681f3Smrg *
247ec681f3Smrg *********************************************************
257ec681f3Smrg * Authors:
267ec681f3Smrg * Zack Rusin <zackr-at-vmware-dot-com>
277ec681f3Smrg * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
287ec681f3Smrg */
297ec681f3Smrg
307ec681f3Smrg#include "xa_context.h"
317ec681f3Smrg#include "xa_priv.h"
327ec681f3Smrg#include "util/u_inlines.h"
337ec681f3Smrg#include "util/u_sampler.h"
347ec681f3Smrg#include "util/u_surface.h"
357ec681f3Smrg#include "cso_cache/cso_context.h"
367ec681f3Smrg
377ec681f3Smrgstatic void
387ec681f3Smrgxa_yuv_bind_blend_state(struct xa_context *r)
397ec681f3Smrg{
407ec681f3Smrg    struct pipe_blend_state blend;
417ec681f3Smrg
427ec681f3Smrg    memset(&blend, 0, sizeof(struct pipe_blend_state));
437ec681f3Smrg    blend.rt[0].blend_enable = 0;
447ec681f3Smrg    blend.rt[0].colormask = PIPE_MASK_RGBA;
457ec681f3Smrg
467ec681f3Smrg    /* porter&duff src */
477ec681f3Smrg    blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
487ec681f3Smrg    blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
497ec681f3Smrg    blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
507ec681f3Smrg    blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
517ec681f3Smrg
527ec681f3Smrg    cso_set_blend(r->cso, &blend);
537ec681f3Smrg}
547ec681f3Smrg
557ec681f3Smrgstatic void
567ec681f3Smrgxa_yuv_bind_shaders(struct xa_context *r)
577ec681f3Smrg{
587ec681f3Smrg    unsigned vs_traits = 0, fs_traits = 0;
597ec681f3Smrg    struct xa_shader shader;
607ec681f3Smrg
617ec681f3Smrg    vs_traits |= VS_YUV;
627ec681f3Smrg    fs_traits |= FS_YUV;
637ec681f3Smrg
647ec681f3Smrg    shader = xa_shaders_get(r->shaders, vs_traits, fs_traits);
657ec681f3Smrg    cso_set_vertex_shader_handle(r->cso, shader.vs);
667ec681f3Smrg    cso_set_fragment_shader_handle(r->cso, shader.fs);
677ec681f3Smrg}
687ec681f3Smrg
697ec681f3Smrgstatic void
707ec681f3Smrgxa_yuv_bind_samplers(struct xa_context *r, struct xa_surface *yuv[])
717ec681f3Smrg{
727ec681f3Smrg    struct pipe_sampler_state *samplers[3];
737ec681f3Smrg    struct pipe_sampler_state sampler;
747ec681f3Smrg    struct pipe_sampler_view view_templ;
757ec681f3Smrg    unsigned int i;
767ec681f3Smrg
777ec681f3Smrg    memset(&sampler, 0, sizeof(struct pipe_sampler_state));
787ec681f3Smrg
797ec681f3Smrg    sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
807ec681f3Smrg    sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
817ec681f3Smrg    sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
827ec681f3Smrg    sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
837ec681f3Smrg    sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
847ec681f3Smrg    sampler.normalized_coords = 1;
857ec681f3Smrg
867ec681f3Smrg    for (i = 0; i < 3; ++i) {
877ec681f3Smrg	samplers[i] = &sampler;
887ec681f3Smrg	u_sampler_view_default_template(&view_templ, yuv[i]->tex,
897ec681f3Smrg					yuv[i]->tex->format);
907ec681f3Smrg
917ec681f3Smrg	r->bound_sampler_views[i] =
927ec681f3Smrg	    r->pipe->create_sampler_view(r->pipe, yuv[i]->tex, &view_templ);
937ec681f3Smrg    }
947ec681f3Smrg    r->num_bound_samplers = 3;
957ec681f3Smrg    cso_set_samplers(r->cso, PIPE_SHADER_FRAGMENT, 3, (const struct pipe_sampler_state **)samplers);
967ec681f3Smrg    r->pipe->set_sampler_views(r->pipe, PIPE_SHADER_FRAGMENT, 0, 3, 0, false, r->bound_sampler_views);
977ec681f3Smrg}
987ec681f3Smrg
997ec681f3Smrgstatic void
1007ec681f3Smrgxa_yuv_fs_constants(struct xa_context *r, const float conversion_matrix[])
1017ec681f3Smrg{
1027ec681f3Smrg    const int param_bytes = 16 * sizeof(float);
1037ec681f3Smrg
1047ec681f3Smrg    renderer_set_constants(r, PIPE_SHADER_FRAGMENT,
1057ec681f3Smrg			   conversion_matrix, param_bytes);
1067ec681f3Smrg}
1077ec681f3Smrg
1087ec681f3SmrgXA_EXPORT int
1097ec681f3Smrgxa_yuv_planar_blit(struct xa_context *r,
1107ec681f3Smrg		   int src_x,
1117ec681f3Smrg		   int src_y,
1127ec681f3Smrg		   int src_w,
1137ec681f3Smrg		   int src_h,
1147ec681f3Smrg		   int dst_x,
1157ec681f3Smrg		   int dst_y,
1167ec681f3Smrg		   int dst_w,
1177ec681f3Smrg		   int dst_h,
1187ec681f3Smrg		   struct xa_box *box,
1197ec681f3Smrg		   unsigned int num_boxes,
1207ec681f3Smrg		   const float conversion_matrix[],
1217ec681f3Smrg		   struct xa_surface *dst, struct xa_surface *yuv[])
1227ec681f3Smrg{
1237ec681f3Smrg    float scale_x;
1247ec681f3Smrg    float scale_y;
1257ec681f3Smrg    int ret;
1267ec681f3Smrg
1277ec681f3Smrg    if (dst_w == 0 || dst_h == 0)
1287ec681f3Smrg	return XA_ERR_NONE;
1297ec681f3Smrg
1307ec681f3Smrg    ret = xa_ctx_srf_create(r, dst);
1317ec681f3Smrg    if (ret != XA_ERR_NONE)
1327ec681f3Smrg	return -XA_ERR_NORES;
1337ec681f3Smrg
1347ec681f3Smrg    renderer_bind_destination(r, r->srf);
1357ec681f3Smrg    xa_yuv_bind_blend_state(r);
1367ec681f3Smrg    xa_yuv_bind_shaders(r);
1377ec681f3Smrg    xa_yuv_bind_samplers(r, yuv);
1387ec681f3Smrg    xa_yuv_fs_constants(r, conversion_matrix);
1397ec681f3Smrg
1407ec681f3Smrg    scale_x = (float)src_w / (float)dst_w;
1417ec681f3Smrg    scale_y = (float)src_h / (float)dst_h;
1427ec681f3Smrg
1437ec681f3Smrg    while (num_boxes--) {
1447ec681f3Smrg	int x = box->x1;
1457ec681f3Smrg	int y = box->y1;
1467ec681f3Smrg	int w = box->x2 - box->x1;
1477ec681f3Smrg	int h = box->y2 - box->y1;
1487ec681f3Smrg
1497ec681f3Smrg        xa_scissor_update(r, x, y, box->x2, box->y2);
1507ec681f3Smrg	renderer_draw_yuv(r,
1517ec681f3Smrg			  (float)src_x + scale_x * (x - dst_x),
1527ec681f3Smrg			  (float)src_y + scale_y * (y - dst_y),
1537ec681f3Smrg			  scale_x * w, scale_y * h, x, y, w, h, yuv);
1547ec681f3Smrg	box++;
1557ec681f3Smrg    }
1567ec681f3Smrg
1577ec681f3Smrg    xa_context_flush(r);
1587ec681f3Smrg
1597ec681f3Smrg    xa_ctx_sampler_views_destroy(r);
1607ec681f3Smrg    xa_ctx_srf_destroy(r);
1617ec681f3Smrg
1627ec681f3Smrg    return XA_ERR_NONE;
1637ec681f3Smrg}
164