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 
37 static void
xa_yuv_bind_blend_state(struct xa_context * r)38 xa_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 
55 static void
xa_yuv_bind_shaders(struct xa_context * r)56 xa_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 
69 static void
xa_yuv_bind_samplers(struct xa_context * r,struct xa_surface * yuv[])70 xa_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 view_templ;
75     unsigned int i;
76 
77     memset(&sampler, 0, sizeof(struct pipe_sampler_state));
78 
79     sampler.wrap_s = PIPE_TEX_WRAP_CLAMP;
80     sampler.wrap_t = PIPE_TEX_WRAP_CLAMP;
81     sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
82     sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
83     sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
84     sampler.normalized_coords = 1;
85 
86     for (i = 0; i < 3; ++i) {
87 	samplers[i] = &sampler;
88 	u_sampler_view_default_template(&view_templ, yuv[i]->tex,
89 					yuv[i]->tex->format);
90 
91 	r->bound_sampler_views[i] =
92 	    r->pipe->create_sampler_view(r->pipe, yuv[i]->tex, &view_templ);
93     }
94     r->num_bound_samplers = 3;
95     cso_set_samplers(r->cso, PIPE_SHADER_FRAGMENT, 3, (const struct pipe_sampler_state **)samplers);
96     cso_set_sampler_views(r->cso, PIPE_SHADER_FRAGMENT, 3, r->bound_sampler_views);
97 }
98 
99 static void
xa_yuv_fs_constants(struct xa_context * r,const float conversion_matrix[])100 xa_yuv_fs_constants(struct xa_context *r, const float conversion_matrix[])
101 {
102     const int param_bytes = 16 * sizeof(float);
103 
104     renderer_set_constants(r, PIPE_SHADER_FRAGMENT,
105 			   conversion_matrix, param_bytes);
106 }
107 
108 XA_EXPORT int
xa_yuv_planar_blit(struct xa_context * r,int src_x,int src_y,int src_w,int src_h,int dst_x,int dst_y,int dst_w,int dst_h,struct xa_box * box,unsigned int num_boxes,const float conversion_matrix[],struct xa_surface * dst,struct xa_surface * yuv[])109 xa_yuv_planar_blit(struct xa_context *r,
110 		   int src_x,
111 		   int src_y,
112 		   int src_w,
113 		   int src_h,
114 		   int dst_x,
115 		   int dst_y,
116 		   int dst_w,
117 		   int dst_h,
118 		   struct xa_box *box,
119 		   unsigned int num_boxes,
120 		   const float conversion_matrix[],
121 		   struct xa_surface *dst, struct xa_surface *yuv[])
122 {
123     float scale_x;
124     float scale_y;
125     int ret;
126 
127     if (dst_w == 0 || dst_h == 0)
128 	return XA_ERR_NONE;
129 
130     ret = xa_ctx_srf_create(r, dst);
131     if (ret != XA_ERR_NONE)
132 	return -XA_ERR_NORES;
133 
134     renderer_bind_destination(r, r->srf, r->srf->width, r->srf->height);
135     xa_yuv_bind_blend_state(r);
136     xa_yuv_bind_shaders(r);
137     xa_yuv_bind_samplers(r, yuv);
138     xa_yuv_fs_constants(r, conversion_matrix);
139 
140     scale_x = (float)src_w / (float)dst_w;
141     scale_y = (float)src_h / (float)dst_h;
142 
143     while (num_boxes--) {
144 	int x = box->x1;
145 	int y = box->y1;
146 	int w = box->x2 - box->x1;
147 	int h = box->y2 - box->y1;
148 
149 	renderer_draw_yuv(r,
150 			  (float)src_x + scale_x * (x - dst_x),
151 			  (float)src_y + scale_y * (y - dst_y),
152 			  scale_x * w, scale_y * h, x, y, w, h, yuv);
153 	box++;
154     }
155 
156     r->pipe->flush(r->pipe, &r->last_fence);
157 
158     xa_ctx_sampler_views_destroy(r);
159     xa_ctx_srf_destroy(r);
160 
161     return XA_ERR_NONE;
162 }
163