• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2009 Younes Manton.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include "util/u_sampler.h"
29 
30 #include "vl_compositor_gfx.h"
31 #include "vl_compositor_cs.h"
32 
33 static bool
init_shaders(struct vl_compositor * c)34 init_shaders(struct vl_compositor *c)
35 {
36    assert(c);
37 
38    if (c->pipe_cs_composit_supported) {
39       if (!vl_compositor_cs_init_shaders(c))
40          return false;
41 
42    } else if (c->pipe_gfx_supported) {
43       c->fs_video_buffer = create_frag_shader_video_buffer(c);
44       if (!c->fs_video_buffer) {
45          debug_printf("Unable to create YCbCr-to-RGB fragment shader.\n");
46          return false;
47       }
48 
49       c->fs_weave_rgb = create_frag_shader_weave_rgb(c);
50       if (!c->fs_weave_rgb) {
51          debug_printf("Unable to create YCbCr-to-RGB weave fragment shader.\n");
52          return false;
53       }
54 
55       c->fs_yuv.weave.y = create_frag_shader_deint_yuv(c, true, true);
56       c->fs_yuv.weave.uv = create_frag_shader_deint_yuv(c, false, true);
57       c->fs_yuv.bob.y = create_frag_shader_deint_yuv(c, true, false);
58       c->fs_yuv.bob.uv = create_frag_shader_deint_yuv(c, false, false);
59       if (!c->fs_yuv.weave.y || !c->fs_yuv.weave.uv ||
60           !c->fs_yuv.bob.y || !c->fs_yuv.bob.uv) {
61          debug_printf("Unable to create YCbCr i-to-YCbCr p deint fragment shader.\n");
62          return false;
63       }
64 
65       c->fs_rgb_yuv.y = create_frag_shader_rgb_yuv(c, true);
66       c->fs_rgb_yuv.uv = create_frag_shader_rgb_yuv(c, false);
67       if (!c->fs_rgb_yuv.y || !c->fs_rgb_yuv.uv) {
68          debug_printf("Unable to create RGB-to-YUV fragment shader.\n");
69          return false;
70       }
71    }
72 
73    if (c->pipe_gfx_supported) {
74       c->vs = create_vert_shader(c);
75       if (!c->vs) {
76          debug_printf("Unable to create vertex shader.\n");
77          return false;
78       }
79 
80       c->fs_palette.yuv = create_frag_shader_palette(c, true);
81       if (!c->fs_palette.yuv) {
82          debug_printf("Unable to create YUV-Palette-to-RGB fragment shader.\n");
83          return false;
84       }
85 
86       c->fs_palette.rgb = create_frag_shader_palette(c, false);
87       if (!c->fs_palette.rgb) {
88          debug_printf("Unable to create RGB-Palette-to-RGB fragment shader.\n");
89          return false;
90       }
91 
92       c->fs_rgba = create_frag_shader_rgba(c);
93       if (!c->fs_rgba) {
94          debug_printf("Unable to create RGB-to-RGB fragment shader.\n");
95          return false;
96       }
97    }
98 
99    return true;
100 }
101 
cleanup_shaders(struct vl_compositor * c)102 static void cleanup_shaders(struct vl_compositor *c)
103 {
104    assert(c);
105 
106    if (c->pipe_cs_composit_supported) {
107       vl_compositor_cs_cleanup_shaders(c);
108    } else if (c->pipe_gfx_supported) {
109       c->pipe->delete_fs_state(c->pipe, c->fs_video_buffer);
110       c->pipe->delete_fs_state(c->pipe, c->fs_weave_rgb);
111       c->pipe->delete_fs_state(c->pipe, c->fs_yuv.weave.y);
112       c->pipe->delete_fs_state(c->pipe, c->fs_yuv.weave.uv);
113       c->pipe->delete_fs_state(c->pipe, c->fs_yuv.bob.y);
114       c->pipe->delete_fs_state(c->pipe, c->fs_yuv.bob.uv);
115       c->pipe->delete_fs_state(c->pipe, c->fs_rgb_yuv.y);
116       c->pipe->delete_fs_state(c->pipe, c->fs_rgb_yuv.uv);
117    }
118 
119    if (c->pipe_gfx_supported) {
120       c->pipe->delete_vs_state(c->pipe, c->vs);
121       c->pipe->delete_fs_state(c->pipe, c->fs_palette.yuv);
122       c->pipe->delete_fs_state(c->pipe, c->fs_palette.rgb);
123       c->pipe->delete_fs_state(c->pipe, c->fs_rgba);
124    }
125 }
126 
127 static bool
init_pipe_state(struct vl_compositor * c)128 init_pipe_state(struct vl_compositor *c)
129 {
130    struct pipe_rasterizer_state rast;
131    struct pipe_sampler_state sampler;
132    struct pipe_blend_state blend;
133    struct pipe_depth_stencil_alpha_state dsa;
134    unsigned i;
135 
136    assert(c);
137 
138    c->fb_state.nr_cbufs = 1;
139    c->fb_state.zsbuf = NULL;
140 
141    memset(&sampler, 0, sizeof(sampler));
142    sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
143    sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
144    sampler.wrap_r = PIPE_TEX_WRAP_REPEAT;
145    sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
146    sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
147    sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
148    sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
149    sampler.compare_func = PIPE_FUNC_ALWAYS;
150 
151    c->sampler_linear = c->pipe->create_sampler_state(c->pipe, &sampler);
152 
153    sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
154    sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
155    c->sampler_nearest = c->pipe->create_sampler_state(c->pipe, &sampler);
156 
157    if (c->pipe_gfx_supported) {
158            memset(&blend, 0, sizeof blend);
159            blend.independent_blend_enable = 0;
160            blend.rt[0].blend_enable = 0;
161            blend.logicop_enable = 0;
162            blend.logicop_func = PIPE_LOGICOP_CLEAR;
163            blend.rt[0].colormask = PIPE_MASK_RGBA;
164            blend.dither = 0;
165            c->blend_clear = c->pipe->create_blend_state(c->pipe, &blend);
166 
167            blend.rt[0].blend_enable = 1;
168            blend.rt[0].rgb_func = PIPE_BLEND_ADD;
169            blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
170            blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
171            blend.rt[0].alpha_func = PIPE_BLEND_ADD;
172            blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
173            blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
174            c->blend_add = c->pipe->create_blend_state(c->pipe, &blend);
175 
176            memset(&rast, 0, sizeof rast);
177            rast.flatshade = 0;
178            rast.front_ccw = 1;
179            rast.cull_face = PIPE_FACE_NONE;
180            rast.fill_back = PIPE_POLYGON_MODE_FILL;
181            rast.fill_front = PIPE_POLYGON_MODE_FILL;
182            rast.scissor = 1;
183            rast.line_width = 1;
184            rast.point_size_per_vertex = 1;
185            rast.offset_units = 1;
186            rast.offset_scale = 1;
187            rast.half_pixel_center = 1;
188            rast.bottom_edge_rule = 1;
189            rast.depth_clip_near = 1;
190            rast.depth_clip_far = 1;
191 
192            c->rast = c->pipe->create_rasterizer_state(c->pipe, &rast);
193 
194            memset(&dsa, 0, sizeof dsa);
195            dsa.depth_enabled = 0;
196            dsa.depth_writemask = 0;
197            dsa.depth_func = PIPE_FUNC_ALWAYS;
198            for (i = 0; i < 2; ++i) {
199                    dsa.stencil[i].enabled = 0;
200                    dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
201                    dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
202                    dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
203                    dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
204                    dsa.stencil[i].valuemask = 0;
205                    dsa.stencil[i].writemask = 0;
206            }
207            dsa.alpha_enabled = 0;
208            dsa.alpha_func = PIPE_FUNC_ALWAYS;
209            dsa.alpha_ref_value = 0;
210            c->dsa = c->pipe->create_depth_stencil_alpha_state(c->pipe, &dsa);
211            c->pipe->bind_depth_stencil_alpha_state(c->pipe, c->dsa);
212    }
213 
214    return true;
215 }
216 
cleanup_pipe_state(struct vl_compositor * c)217 static void cleanup_pipe_state(struct vl_compositor *c)
218 {
219    assert(c);
220 
221    if (c->pipe_gfx_supported) {
222            /* Asserted in softpipe_delete_fs_state() for some reason */
223            c->pipe->bind_vs_state(c->pipe, NULL);
224            c->pipe->bind_fs_state(c->pipe, NULL);
225 
226            c->pipe->delete_depth_stencil_alpha_state(c->pipe, c->dsa);
227            c->pipe->delete_blend_state(c->pipe, c->blend_clear);
228            c->pipe->delete_blend_state(c->pipe, c->blend_add);
229            c->pipe->delete_rasterizer_state(c->pipe, c->rast);
230    }
231    c->pipe->delete_sampler_state(c->pipe, c->sampler_linear);
232    c->pipe->delete_sampler_state(c->pipe, c->sampler_nearest);
233 }
234 
235 static bool
init_buffers(struct vl_compositor * c)236 init_buffers(struct vl_compositor *c)
237 {
238    struct pipe_vertex_element vertex_elems[3];
239    memset(vertex_elems, 0, sizeof(vertex_elems));
240 
241    assert(c);
242 
243    /*
244     * Create our vertex buffer and vertex buffer elements
245     */
246    c->vertex_buf.buffer_offset = 0;
247    c->vertex_buf.buffer.resource = NULL;
248    c->vertex_buf.is_user_buffer = false;
249 
250    if (c->pipe_gfx_supported) {
251            vertex_elems[0].src_offset = 0;
252            vertex_elems[0].src_stride = VL_COMPOSITOR_VB_STRIDE;
253            vertex_elems[0].instance_divisor = 0;
254            vertex_elems[0].vertex_buffer_index = 0;
255            vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT;
256            vertex_elems[1].src_offset = sizeof(struct vertex2f);
257            vertex_elems[1].src_stride = VL_COMPOSITOR_VB_STRIDE;
258            vertex_elems[1].instance_divisor = 0;
259            vertex_elems[1].vertex_buffer_index = 0;
260            vertex_elems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
261            vertex_elems[2].src_offset = sizeof(struct vertex2f) + sizeof(struct vertex4f);
262            vertex_elems[1].src_stride = VL_COMPOSITOR_VB_STRIDE;
263            vertex_elems[2].instance_divisor = 0;
264            vertex_elems[2].vertex_buffer_index = 0;
265            vertex_elems[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
266            c->vertex_elems_state = c->pipe->create_vertex_elements_state(c->pipe, 3, vertex_elems);
267    }
268 
269    return true;
270 }
271 
272 static void
cleanup_buffers(struct vl_compositor * c)273 cleanup_buffers(struct vl_compositor *c)
274 {
275    assert(c);
276 
277    if (c->pipe_gfx_supported) {
278            c->pipe->delete_vertex_elements_state(c->pipe, c->vertex_elems_state);
279    }
280    pipe_resource_reference(&c->vertex_buf.buffer.resource, NULL);
281 }
282 
283 static inline struct u_rect
default_rect(struct vl_compositor_layer * layer)284 default_rect(struct vl_compositor_layer *layer)
285 {
286    struct pipe_resource *res = layer->sampler_views[0]->texture;
287    struct u_rect rect = { 0, res->width0, 0, res->height0 * res->array_size };
288    return rect;
289 }
290 
291 static inline struct vertex2f
calc_topleft(struct vertex2f size,struct u_rect rect)292 calc_topleft(struct vertex2f size, struct u_rect rect)
293 {
294    struct vertex2f res = { rect.x0 / size.x, rect.y0 / size.y };
295    return res;
296 }
297 
298 static inline struct vertex2f
calc_bottomright(struct vertex2f size,struct u_rect rect)299 calc_bottomright(struct vertex2f size, struct u_rect rect)
300 {
301    struct vertex2f res = { rect.x1 / size.x, rect.y1 / size.y };
302    return res;
303 }
304 
305 static inline void
calc_src_and_dst(struct vl_compositor_layer * layer,unsigned width,unsigned height,struct u_rect src,struct u_rect dst)306 calc_src_and_dst(struct vl_compositor_layer *layer, unsigned width, unsigned height,
307                  struct u_rect src, struct u_rect dst)
308 {
309    struct vertex2f size =  { width, height };
310 
311    layer->src.tl = calc_topleft(size, src);
312    layer->src.br = calc_bottomright(size, src);
313    layer->dst.tl = calc_topleft(size, dst);
314    layer->dst.br = calc_bottomright(size, dst);
315    layer->zw.x = 0.0f;
316    layer->zw.y = size.y;
317 }
318 
319 static void
set_yuv_layer(struct vl_compositor_state * s,struct vl_compositor * c,unsigned layer,struct pipe_video_buffer * buffer,struct u_rect * src_rect,struct u_rect * dst_rect,bool y,enum vl_compositor_deinterlace deinterlace)320 set_yuv_layer(struct vl_compositor_state *s, struct vl_compositor *c,
321               unsigned layer, struct pipe_video_buffer *buffer,
322               struct u_rect *src_rect, struct u_rect *dst_rect,
323               bool y, enum vl_compositor_deinterlace deinterlace)
324 {
325    struct pipe_sampler_view **sampler_views;
326    float half_a_line;
327    unsigned i;
328 
329    assert(s && c && buffer);
330 
331    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
332 
333    s->used_layers |= 1 << layer;
334    sampler_views = buffer->get_sampler_view_components(buffer);
335    for (i = 0; i < 3; ++i) {
336       s->layers[layer].samplers[i] = c->sampler_linear;
337       pipe_sampler_view_reference(&s->layers[layer].sampler_views[i], sampler_views[i]);
338    }
339 
340    calc_src_and_dst(&s->layers[layer], buffer->width, buffer->height,
341                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
342                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
343 
344    half_a_line = 0.5f / s->layers[layer].zw.y;
345 
346    switch(deinterlace) {
347    case VL_COMPOSITOR_BOB_TOP:
348       s->layers[layer].zw.x = 0.0f;
349       s->layers[layer].src.tl.y += half_a_line;
350       s->layers[layer].src.br.y += half_a_line;
351       if (c->pipe_gfx_supported)
352           s->layers[layer].fs = (y) ? c->fs_yuv.bob.y : c->fs_yuv.bob.uv;
353       if (c->pipe_cs_composit_supported)
354           s->layers[layer].cs = (y) ? c->cs_yuv.progressive.y : c->cs_yuv.progressive.uv;
355       break;
356 
357    case VL_COMPOSITOR_BOB_BOTTOM:
358       s->layers[layer].zw.x = 1.0f;
359       s->layers[layer].src.tl.y -= half_a_line;
360       s->layers[layer].src.br.y -= half_a_line;
361       if (c->pipe_gfx_supported)
362           s->layers[layer].fs = (y) ? c->fs_yuv.bob.y : c->fs_yuv.bob.uv;
363       if (c->pipe_cs_composit_supported)
364           s->layers[layer].cs = (y) ? c->cs_yuv.progressive.y : c->cs_yuv.progressive.uv;
365       break;
366 
367    case VL_COMPOSITOR_NONE:
368       if (c->pipe_cs_composit_supported) {
369           s->layers[layer].cs = (y) ? c->cs_yuv.progressive.y : c->cs_yuv.progressive.uv;
370           break;
371       }
372       FALLTHROUGH;
373 
374    default:
375       if (c->pipe_gfx_supported)
376           s->layers[layer].fs = (y) ? c->fs_yuv.weave.y : c->fs_yuv.weave.uv;
377       if (c->pipe_cs_composit_supported)
378           s->layers[layer].cs = (y) ? c->cs_yuv.weave.y : c->cs_yuv.weave.uv;
379       break;
380    }
381 }
382 
383 static void
set_rgb_to_yuv_layer(struct vl_compositor_state * s,struct vl_compositor * c,unsigned layer,struct pipe_sampler_view * v,struct u_rect * src_rect,struct u_rect * dst_rect,bool y)384 set_rgb_to_yuv_layer(struct vl_compositor_state *s, struct vl_compositor *c,
385                      unsigned layer, struct pipe_sampler_view *v,
386                      struct u_rect *src_rect, struct u_rect *dst_rect, bool y)
387 {
388    assert(s && c && v);
389 
390    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
391 
392    s->used_layers |= 1 << layer;
393 
394    if (c->pipe_cs_composit_supported)
395       s->layers[layer].cs = y ? c->cs_rgb_yuv.y : c->cs_rgb_yuv.uv;
396    else if (c->pipe_gfx_supported)
397       s->layers[layer].fs = y ? c->fs_rgb_yuv.y : c->fs_rgb_yuv.uv;
398 
399    s->layers[layer].samplers[0] = c->sampler_linear;
400    s->layers[layer].samplers[1] = NULL;
401    s->layers[layer].samplers[2] = NULL;
402 
403    pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], v);
404    pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], NULL);
405    pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);
406 
407    calc_src_and_dst(&s->layers[layer], v->texture->width0, v->texture->height0,
408                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
409                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
410 }
411 
412 void
vl_compositor_reset_dirty_area(struct u_rect * dirty)413 vl_compositor_reset_dirty_area(struct u_rect *dirty)
414 {
415    assert(dirty);
416 
417    dirty->x0 = dirty->y0 = VL_COMPOSITOR_MIN_DIRTY;
418    dirty->x1 = dirty->y1 = VL_COMPOSITOR_MAX_DIRTY;
419 }
420 
421 void
vl_compositor_set_clear_color(struct vl_compositor_state * s,union pipe_color_union * color)422 vl_compositor_set_clear_color(struct vl_compositor_state *s, union pipe_color_union *color)
423 {
424    assert(s);
425    assert(color);
426 
427    s->clear_color = *color;
428 }
429 
430 void
vl_compositor_get_clear_color(struct vl_compositor_state * s,union pipe_color_union * color)431 vl_compositor_get_clear_color(struct vl_compositor_state *s, union pipe_color_union *color)
432 {
433    assert(s);
434    assert(color);
435 
436    *color = s->clear_color;
437 }
438 
439 void
vl_compositor_clear_layers(struct vl_compositor_state * s)440 vl_compositor_clear_layers(struct vl_compositor_state *s)
441 {
442    unsigned i, j;
443 
444    assert(s);
445    s->used_layers = 0;
446    for ( i = 0; i < VL_COMPOSITOR_MAX_LAYERS; ++i) {
447       struct vertex4f v_one = { 1.0f, 1.0f, 1.0f, 1.0f };
448       s->layers[i].clearing = i ? false : true;
449       s->layers[i].blend = NULL;
450       s->layers[i].fs = NULL;
451       s->layers[i].cs = NULL;
452       s->layers[i].viewport.scale[2] = 1;
453       s->layers[i].viewport.translate[2] = 0;
454       s->layers[i].viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
455       s->layers[i].viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
456       s->layers[i].viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
457       s->layers[i].viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
458       s->layers[i].rotate = VL_COMPOSITOR_ROTATE_0;
459 
460       for ( j = 0; j < 3; j++)
461          pipe_sampler_view_reference(&s->layers[i].sampler_views[j], NULL);
462       for ( j = 0; j < 4; ++j)
463          s->layers[i].colors[j] = v_one;
464    }
465 }
466 
467 void
vl_compositor_cleanup(struct vl_compositor * c)468 vl_compositor_cleanup(struct vl_compositor *c)
469 {
470    assert(c);
471 
472    cleanup_buffers(c);
473    cleanup_shaders(c);
474    cleanup_pipe_state(c);
475 }
476 
477 bool
vl_compositor_set_csc_matrix(struct vl_compositor_state * s,vl_csc_matrix const * matrix,float luma_min,float luma_max)478 vl_compositor_set_csc_matrix(struct vl_compositor_state *s,
479                              vl_csc_matrix const *matrix,
480                              float luma_min, float luma_max)
481 {
482    assert(s);
483 
484    memcpy(&s->csc_matrix, matrix, sizeof(vl_csc_matrix));
485    s->luma_min = luma_min;
486    s->luma_max = luma_max;
487 
488    return true;
489 }
490 
491 void
vl_compositor_set_dst_clip(struct vl_compositor_state * s,struct u_rect * dst_clip)492 vl_compositor_set_dst_clip(struct vl_compositor_state *s, struct u_rect *dst_clip)
493 {
494    assert(s);
495 
496    s->scissor_valid = dst_clip != NULL;
497    if (dst_clip) {
498       s->scissor.minx = dst_clip->x0;
499       s->scissor.miny = dst_clip->y0;
500       s->scissor.maxx = dst_clip->x1;
501       s->scissor.maxy = dst_clip->y1;
502    }
503 }
504 
505 void
vl_compositor_set_layer_blend(struct vl_compositor_state * s,unsigned layer,void * blend,bool is_clearing)506 vl_compositor_set_layer_blend(struct vl_compositor_state *s,
507                               unsigned layer, void *blend,
508                               bool is_clearing)
509 {
510    assert(s && blend);
511 
512    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
513 
514    s->layers[layer].clearing = is_clearing;
515    s->layers[layer].blend = blend;
516 }
517 
518 void
vl_compositor_set_layer_dst_area(struct vl_compositor_state * s,unsigned layer,struct u_rect * dst_area)519 vl_compositor_set_layer_dst_area(struct vl_compositor_state *s,
520                                  unsigned layer, struct u_rect *dst_area)
521 {
522    assert(s);
523 
524    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
525 
526    s->layers[layer].viewport_valid = dst_area != NULL;
527    if (dst_area) {
528       s->layers[layer].viewport.scale[0] = dst_area->x1 - dst_area->x0;
529       s->layers[layer].viewport.scale[1] = dst_area->y1 - dst_area->y0;
530       s->layers[layer].viewport.translate[0] = dst_area->x0;
531       s->layers[layer].viewport.translate[1] = dst_area->y0;
532    }
533 }
534 
535 void
vl_compositor_set_buffer_layer(struct vl_compositor_state * s,struct vl_compositor * c,unsigned layer,struct pipe_video_buffer * buffer,struct u_rect * src_rect,struct u_rect * dst_rect,enum vl_compositor_deinterlace deinterlace)536 vl_compositor_set_buffer_layer(struct vl_compositor_state *s,
537                                struct vl_compositor *c,
538                                unsigned layer,
539                                struct pipe_video_buffer *buffer,
540                                struct u_rect *src_rect,
541                                struct u_rect *dst_rect,
542                                enum vl_compositor_deinterlace deinterlace)
543 {
544    struct pipe_sampler_view **sampler_views;
545    unsigned i;
546 
547    assert(s && c && buffer);
548 
549    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
550 
551    s->used_layers |= 1 << layer;
552    sampler_views = buffer->get_sampler_view_components(buffer);
553    for (i = 0; i < 3; ++i) {
554       s->layers[layer].samplers[i] = c->sampler_linear;
555       pipe_sampler_view_reference(&s->layers[layer].sampler_views[i], sampler_views[i]);
556    }
557 
558    calc_src_and_dst(&s->layers[layer], buffer->width, buffer->height,
559                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
560                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
561 
562    if (buffer->interlaced) {
563       float half_a_line = 0.5f / s->layers[layer].zw.y;
564       switch(deinterlace) {
565       case VL_COMPOSITOR_NONE:
566       case VL_COMPOSITOR_MOTION_ADAPTIVE:
567       case VL_COMPOSITOR_WEAVE:
568          if (c->pipe_cs_composit_supported)
569             s->layers[layer].cs = c->cs_weave_rgb;
570          else if (c->pipe_gfx_supported)
571             s->layers[layer].fs = c->fs_weave_rgb;
572          break;
573 
574       case VL_COMPOSITOR_BOB_TOP:
575          s->layers[layer].zw.x = 0.0f;
576          s->layers[layer].src.tl.y += half_a_line;
577          s->layers[layer].src.br.y += half_a_line;
578          if (c->pipe_cs_composit_supported)
579             s->layers[layer].cs = c->cs_video_buffer;
580          else if (c->pipe_gfx_supported)
581             s->layers[layer].fs = c->fs_video_buffer;
582          break;
583 
584       case VL_COMPOSITOR_BOB_BOTTOM:
585          s->layers[layer].zw.x = 1.0f;
586          s->layers[layer].src.tl.y -= half_a_line;
587          s->layers[layer].src.br.y -= half_a_line;
588          if (c->pipe_cs_composit_supported)
589             s->layers[layer].cs = c->cs_video_buffer;
590          else if (c->pipe_gfx_supported)
591             s->layers[layer].fs = c->fs_video_buffer;
592          break;
593       }
594 
595    } else {
596       if (c->pipe_cs_composit_supported)
597          s->layers[layer].cs = c->cs_video_buffer;
598       else if (c->pipe_gfx_supported)
599          s->layers[layer].fs = c->fs_video_buffer;
600    }
601 }
602 
603 void
vl_compositor_set_palette_layer(struct vl_compositor_state * s,struct vl_compositor * c,unsigned layer,struct pipe_sampler_view * indexes,struct pipe_sampler_view * palette,struct u_rect * src_rect,struct u_rect * dst_rect,bool include_color_conversion)604 vl_compositor_set_palette_layer(struct vl_compositor_state *s,
605                                 struct vl_compositor *c,
606                                 unsigned layer,
607                                 struct pipe_sampler_view *indexes,
608                                 struct pipe_sampler_view *palette,
609                                 struct u_rect *src_rect,
610                                 struct u_rect *dst_rect,
611                                 bool include_color_conversion)
612 {
613    assert(s && c && indexes && palette);
614 
615    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
616 
617    s->used_layers |= 1 << layer;
618 
619    s->layers[layer].fs = include_color_conversion ?
620       c->fs_palette.yuv : c->fs_palette.rgb;
621 
622    s->layers[layer].samplers[0] = c->sampler_linear;
623    s->layers[layer].samplers[1] = c->sampler_nearest;
624    s->layers[layer].samplers[2] = NULL;
625    pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], indexes);
626    pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], palette);
627    pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);
628    calc_src_and_dst(&s->layers[layer], indexes->texture->width0, indexes->texture->height0,
629                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
630                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
631 }
632 
633 void
vl_compositor_set_rgba_layer(struct vl_compositor_state * s,struct vl_compositor * c,unsigned layer,struct pipe_sampler_view * rgba,struct u_rect * src_rect,struct u_rect * dst_rect,struct vertex4f * colors)634 vl_compositor_set_rgba_layer(struct vl_compositor_state *s,
635                              struct vl_compositor *c,
636                              unsigned layer,
637                              struct pipe_sampler_view *rgba,
638                              struct u_rect *src_rect,
639                              struct u_rect *dst_rect,
640                              struct vertex4f *colors)
641 {
642    unsigned i;
643 
644    assert(s && c && rgba);
645 
646    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
647 
648    s->used_layers |= 1 << layer;
649    s->layers[layer].fs = c->fs_rgba;
650    s->layers[layer].samplers[0] = c->sampler_linear;
651    s->layers[layer].samplers[1] = NULL;
652    s->layers[layer].samplers[2] = NULL;
653    pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], rgba);
654    pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], NULL);
655    pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);
656    calc_src_and_dst(&s->layers[layer], rgba->texture->width0, rgba->texture->height0,
657                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
658                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
659 
660    if (colors)
661       for (i = 0; i < 4; ++i)
662          s->layers[layer].colors[i] = colors[i];
663 }
664 
665 void
vl_compositor_set_layer_rotation(struct vl_compositor_state * s,unsigned layer,enum vl_compositor_rotation rotate)666 vl_compositor_set_layer_rotation(struct vl_compositor_state *s,
667                                  unsigned layer,
668                                  enum vl_compositor_rotation rotate)
669 {
670    assert(s);
671    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
672    s->layers[layer].rotate = rotate;
673 }
674 
675 void
vl_compositor_yuv_deint_full(struct vl_compositor_state * s,struct vl_compositor * c,struct pipe_video_buffer * src,struct pipe_video_buffer * dst,struct u_rect * src_rect,struct u_rect * dst_rect,enum vl_compositor_deinterlace deinterlace)676 vl_compositor_yuv_deint_full(struct vl_compositor_state *s,
677                              struct vl_compositor *c,
678                              struct pipe_video_buffer *src,
679                              struct pipe_video_buffer *dst,
680                              struct u_rect *src_rect,
681                              struct u_rect *dst_rect,
682                              enum vl_compositor_deinterlace deinterlace)
683 {
684    struct pipe_surface **dst_surfaces;
685 
686    dst_surfaces = dst->get_surfaces(dst);
687    vl_compositor_clear_layers(s);
688 
689    set_yuv_layer(s, c, 0, src, src_rect, NULL, true, deinterlace);
690    vl_compositor_set_layer_dst_area(s, 0, dst_rect);
691    vl_compositor_render(s, c, dst_surfaces[0], NULL, false);
692 
693    if (dst_rect) {
694       dst_rect->x0 /= 2;
695       dst_rect->y0 /= 2;
696       dst_rect->x1 /= 2;
697       dst_rect->y1 /= 2;
698    }
699 
700    set_yuv_layer(s, c, 0, src, src_rect, NULL, false, deinterlace);
701    vl_compositor_set_layer_dst_area(s, 0, dst_rect);
702    vl_compositor_render(s, c, dst_surfaces[1], NULL, false);
703 
704    s->pipe->flush(s->pipe, NULL, 0);
705 }
706 
707 void
vl_compositor_convert_rgb_to_yuv(struct vl_compositor_state * s,struct vl_compositor * c,unsigned layer,struct pipe_resource * src_res,struct pipe_video_buffer * dst,struct u_rect * src_rect,struct u_rect * dst_rect)708 vl_compositor_convert_rgb_to_yuv(struct vl_compositor_state *s,
709                                  struct vl_compositor *c,
710                                  unsigned layer,
711                                  struct pipe_resource *src_res,
712                                  struct pipe_video_buffer *dst,
713                                  struct u_rect *src_rect,
714                                  struct u_rect *dst_rect)
715 {
716    struct pipe_sampler_view *sv, sv_templ;
717    struct pipe_surface **dst_surfaces;
718 
719    dst_surfaces = dst->get_surfaces(dst);
720 
721    memset(&sv_templ, 0, sizeof(sv_templ));
722    u_sampler_view_default_template(&sv_templ, src_res, src_res->format);
723    sv = s->pipe->create_sampler_view(s->pipe, src_res, &sv_templ);
724 
725    vl_compositor_clear_layers(s);
726 
727    set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, true);
728    vl_compositor_set_layer_dst_area(s, 0, dst_rect);
729    vl_compositor_render(s, c, dst_surfaces[0], NULL, false);
730 
731    if (dst_rect) {
732       dst_rect->x0 /= 2;
733       dst_rect->y0 /= 2;
734       dst_rect->x1 /= 2;
735       dst_rect->y1 /= 2;
736    }
737 
738    set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, false);
739    vl_compositor_set_layer_dst_area(s, 0, dst_rect);
740    vl_compositor_render(s, c, dst_surfaces[1], NULL, false);
741    pipe_sampler_view_reference(&sv, NULL);
742 
743    s->pipe->flush(s->pipe, NULL, 0);
744 }
745 
746 void
vl_compositor_render(struct vl_compositor_state * s,struct vl_compositor * c,struct pipe_surface * dst_surface,struct u_rect * dirty_area,bool clear_dirty)747 vl_compositor_render(struct vl_compositor_state *s,
748                      struct vl_compositor       *c,
749                      struct pipe_surface        *dst_surface,
750                      struct u_rect              *dirty_area,
751                      bool                        clear_dirty)
752 {
753    assert(s);
754 
755    if (s->layers->cs)
756       vl_compositor_cs_render(s, c, dst_surface, dirty_area, clear_dirty);
757    else if (s->layers->fs)
758       vl_compositor_gfx_render(s, c, dst_surface, dirty_area, clear_dirty);
759    else
760       debug_warning("Hardware don't support.\n");;
761 }
762 
763 bool
vl_compositor_init(struct vl_compositor * c,struct pipe_context * pipe)764 vl_compositor_init(struct vl_compositor *c, struct pipe_context *pipe)
765 {
766    assert(c);
767 
768    memset(c, 0, sizeof(*c));
769 
770    c->pipe_cs_composit_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_PREFER_COMPUTE_FOR_MULTIMEDIA) &&
771             pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_TEX_TXF_LZ) &&
772             pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_DIV);
773 
774    c->pipe_gfx_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_GRAPHICS);
775    c->pipe = pipe;
776 
777    c->deinterlace = VL_COMPOSITOR_NONE;
778 
779    if (!init_pipe_state(c)) {
780       return false;
781    }
782 
783    if (!init_shaders(c)) {
784       cleanup_pipe_state(c);
785       return false;
786    }
787 
788    if (!init_buffers(c)) {
789       cleanup_shaders(c);
790       cleanup_pipe_state(c);
791       return false;
792    }
793 
794    return true;
795 }
796 
797 bool
vl_compositor_init_state(struct vl_compositor_state * s,struct pipe_context * pipe)798 vl_compositor_init_state(struct vl_compositor_state *s, struct pipe_context *pipe)
799 {
800    vl_csc_matrix csc_matrix;
801 
802    assert(s);
803 
804    memset(s, 0, sizeof(*s));
805 
806    s->pipe = pipe;
807 
808    s->clear_color.f[0] = s->clear_color.f[1] = 0.0f;
809    s->clear_color.f[2] = s->clear_color.f[3] = 0.0f;
810 
811    /*
812     * Create our fragment shader's constant buffer
813     * Const buffer contains the color conversion matrix and bias vectors
814     */
815    /* XXX: Create with IMMUTABLE/STATIC... although it does change every once in a long while... */
816    s->shader_params = pipe_buffer_create_const0
817    (
818       pipe->screen,
819       PIPE_BIND_CONSTANT_BUFFER,
820       PIPE_USAGE_DEFAULT,
821       sizeof(csc_matrix) + 16*sizeof(float) + 2*sizeof(int)
822    );
823 
824    if (!s->shader_params)
825       return false;
826 
827    vl_compositor_clear_layers(s);
828 
829    vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, &csc_matrix);
830    if (!vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix, 1.0f, 0.0f))
831       return false;
832 
833    return true;
834 }
835 
836 void
vl_compositor_cleanup_state(struct vl_compositor_state * s)837 vl_compositor_cleanup_state(struct vl_compositor_state *s)
838 {
839    assert(s);
840 
841    vl_compositor_clear_layers(s);
842    pipe_resource_reference(&s->shader_params, NULL);
843 }
844