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