xa_yuv.c revision a18ffcd40d172835b30ef46d8547755e4d91bbe2
1/**********************************************************
2 * Copyright 2009-2011 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 *********************************************************
25 * Authors:
26 * Zack Rusin <zackr-at-vmware-dot-com>
27 * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
28 */
29
30#include "xa_context.h"
31#include "xa_priv.h"
32#include "util/u_inlines.h"
33#include "util/u_sampler.h"
34#include "util/u_surface.h"
35#include "cso_cache/cso_context.h"
36
37static void
38xa_yuv_bind_blend_state(struct xa_context *r)
39{
40    struct pipe_blend_state blend;
41
42    memset(&blend, 0, sizeof(struct pipe_blend_state));
43    blend.rt[0].blend_enable = 0;
44    blend.rt[0].colormask = PIPE_MASK_RGBA;
45
46    /* porter&duff src */
47    blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
48    blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
49    blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
50    blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
51
52    cso_set_blend(r->cso, &blend);
53}
54
55static void
56xa_yuv_bind_shaders(struct xa_context *r)
57{
58    unsigned vs_traits = 0, fs_traits = 0;
59    struct xa_shader shader;
60
61    vs_traits |= VS_YUV;
62    fs_traits |= FS_YUV;
63
64    shader = xa_shaders_get(r->shaders, vs_traits, fs_traits);
65    cso_set_vertex_shader_handle(r->cso, shader.vs);
66    cso_set_fragment_shader_handle(r->cso, shader.fs);
67}
68
69static void
70xa_yuv_bind_samplers(struct xa_context *r, struct xa_surface *yuv[])
71{
72    struct pipe_sampler_state *samplers[3];
73    struct pipe_sampler_state sampler;
74    struct pipe_sampler_view *views[3];
75    struct pipe_sampler_view view_templ;
76    unsigned int i;
77
78    memset(&sampler, 0, sizeof(struct pipe_sampler_state));
79
80    sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
81    sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
82    sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
83    sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
84    sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
85    sampler.normalized_coords = 1;
86
87    for (i = 0; i < 3; ++i) {
88	samplers[i] = &sampler;
89	if (!yuv[i]->view) {
90	    u_sampler_view_default_template(&view_templ,
91					    yuv[i]->tex, yuv[i]->tex->format);
92
93	    yuv[i]->view = r->pipe->create_sampler_view(r->pipe,
94							yuv[i]->tex,
95							&view_templ);
96	}
97	views[i] = yuv[i]->view;
98    }
99
100    cso_set_samplers(r->cso, 3, (const struct pipe_sampler_state **)samplers);
101    cso_set_fragment_sampler_views(r->cso, 3, views);
102}
103
104static void
105xa_yuv_fs_constants(struct xa_context *r, const float conversion_matrix[])
106{
107    const int param_bytes = 12 * sizeof(float);
108
109    renderer_set_constants(r, PIPE_SHADER_FRAGMENT,
110			   conversion_matrix, param_bytes);
111}
112
113static void
114xa_yuv_destroy_sampler_views(struct xa_surface *yuv[])
115{
116    unsigned int i;
117
118    for (i = 0; i < 3; ++i) {
119	pipe_sampler_view_reference(&yuv[i]->view, NULL);
120    }
121}
122
123extern int
124xa_yuv_planar_blit(struct xa_context *r,
125		   int src_x,
126		   int src_y,
127		   int src_w,
128		   int src_h,
129		   int dst_x,
130		   int dst_y,
131		   int dst_w,
132		   int dst_h,
133		   struct xa_box *box,
134		   unsigned int num_boxes,
135		   const float conversion_matrix[],
136		   struct xa_surface *dst, struct xa_surface *yuv[])
137{
138    float scale_x;
139    float scale_y;
140    struct pipe_surface srf_templ;
141
142    if (dst_w == 0 || dst_h == 0)
143	return XA_ERR_NONE;
144
145    memset(&srf_templ, 0, sizeof(srf_templ));
146    u_surface_default_template(&srf_templ, dst->tex, PIPE_BIND_RENDER_TARGET);
147    dst->srf = r->pipe->create_surface(r->pipe, dst->tex, &srf_templ);
148    if (!dst->srf)
149	return -XA_ERR_NORES;
150
151    renderer_bind_destination(r, dst->srf, dst->srf->width, dst->srf->height);
152    xa_yuv_bind_blend_state(r);
153    xa_yuv_bind_shaders(r);
154    xa_yuv_bind_samplers(r, yuv);
155    xa_yuv_fs_constants(r, conversion_matrix);
156
157    scale_x = (float)src_w / (float)dst_w;
158    scale_y = (float)src_h / (float)dst_h;
159
160    while (num_boxes--) {
161	int x = box->x1;
162	int y = box->y1;
163	int w = box->x2 - box->x1;
164	int h = box->y2 - box->y1;
165
166	renderer_draw_yuv(r,
167			  (float)src_x + scale_x * (x - dst_x),
168			  (float)src_y + scale_y * (y - dst_y),
169			  scale_x * w, scale_y * h, x, y, w, h, yuv);
170	box++;
171    }
172
173    r->pipe->flush(r->pipe, &r->last_fence);
174
175    xa_yuv_destroy_sampler_views(yuv);
176    pipe_surface_reference(&dst->srf, NULL);
177
178    return XA_ERR_NONE;
179}
180