• 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 
66    if (c->pipe_gfx_supported) {
67       c->vs = create_vert_shader(c);
68       if (!c->vs) {
69          debug_printf("Unable to create vertex shader.\n");
70          return false;
71       }
72 
73       c->fs_palette.yuv = create_frag_shader_palette(c, true);
74       if (!c->fs_palette.yuv) {
75          debug_printf("Unable to create YUV-Palette-to-RGB fragment shader.\n");
76          return false;
77       }
78 
79       c->fs_palette.rgb = create_frag_shader_palette(c, false);
80       if (!c->fs_palette.rgb) {
81          debug_printf("Unable to create RGB-Palette-to-RGB fragment shader.\n");
82          return false;
83       }
84 
85       c->fs_rgb_yuv.y = create_frag_shader_rgb_yuv(c, true);
86       c->fs_rgb_yuv.uv = create_frag_shader_rgb_yuv(c, false);
87       if (!c->fs_rgb_yuv.y || !c->fs_rgb_yuv.uv) {
88          debug_printf("Unable to create RGB-to-YUV 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    }
116 
117    if (c->pipe_gfx_supported) {
118       c->pipe->delete_vs_state(c->pipe, c->vs);
119       c->pipe->delete_fs_state(c->pipe, c->fs_palette.yuv);
120       c->pipe->delete_fs_state(c->pipe, c->fs_palette.rgb);
121       c->pipe->delete_fs_state(c->pipe, c->fs_rgb_yuv.y);
122       c->pipe->delete_fs_state(c->pipe, c->fs_rgb_yuv.uv);
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    sampler.normalized_coords = 1;
151 
152    c->sampler_linear = c->pipe->create_sampler_state(c->pipe, &sampler);
153 
154    sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
155    sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
156    c->sampler_nearest = c->pipe->create_sampler_state(c->pipe, &sampler);
157 
158    if (c->pipe_gfx_supported) {
159            memset(&blend, 0, sizeof blend);
160            blend.independent_blend_enable = 0;
161            blend.rt[0].blend_enable = 0;
162            blend.logicop_enable = 0;
163            blend.logicop_func = PIPE_LOGICOP_CLEAR;
164            blend.rt[0].colormask = PIPE_MASK_RGBA;
165            blend.dither = 0;
166            c->blend_clear = c->pipe->create_blend_state(c->pipe, &blend);
167 
168            blend.rt[0].blend_enable = 1;
169            blend.rt[0].rgb_func = PIPE_BLEND_ADD;
170            blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
171            blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
172            blend.rt[0].alpha_func = PIPE_BLEND_ADD;
173            blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
174            blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
175            c->blend_add = c->pipe->create_blend_state(c->pipe, &blend);
176 
177            memset(&rast, 0, sizeof rast);
178            rast.flatshade = 0;
179            rast.front_ccw = 1;
180            rast.cull_face = PIPE_FACE_NONE;
181            rast.fill_back = PIPE_POLYGON_MODE_FILL;
182            rast.fill_front = PIPE_POLYGON_MODE_FILL;
183            rast.scissor = 1;
184            rast.line_width = 1;
185            rast.point_size_per_vertex = 1;
186            rast.offset_units = 1;
187            rast.offset_scale = 1;
188            rast.half_pixel_center = 1;
189            rast.bottom_edge_rule = 1;
190            rast.depth_clip_near = 1;
191            rast.depth_clip_far = 1;
192 
193            c->rast = c->pipe->create_rasterizer_state(c->pipe, &rast);
194 
195            memset(&dsa, 0, sizeof dsa);
196            dsa.depth.enabled = 0;
197            dsa.depth.writemask = 0;
198            dsa.depth.func = PIPE_FUNC_ALWAYS;
199            for (i = 0; i < 2; ++i) {
200                    dsa.stencil[i].enabled = 0;
201                    dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
202                    dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
203                    dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
204                    dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
205                    dsa.stencil[i].valuemask = 0;
206                    dsa.stencil[i].writemask = 0;
207            }
208            dsa.alpha.enabled = 0;
209            dsa.alpha.func = PIPE_FUNC_ALWAYS;
210            dsa.alpha.ref_value = 0;
211            c->dsa = c->pipe->create_depth_stencil_alpha_state(c->pipe, &dsa);
212            c->pipe->bind_depth_stencil_alpha_state(c->pipe, c->dsa);
213    }
214 
215    return true;
216 }
217 
cleanup_pipe_state(struct vl_compositor * c)218 static void cleanup_pipe_state(struct vl_compositor *c)
219 {
220    assert(c);
221 
222    if (c->pipe_gfx_supported) {
223            /* Asserted in softpipe_delete_fs_state() for some reason */
224            c->pipe->bind_vs_state(c->pipe, NULL);
225            c->pipe->bind_fs_state(c->pipe, NULL);
226 
227            c->pipe->delete_depth_stencil_alpha_state(c->pipe, c->dsa);
228            c->pipe->delete_blend_state(c->pipe, c->blend_clear);
229            c->pipe->delete_blend_state(c->pipe, c->blend_add);
230            c->pipe->delete_rasterizer_state(c->pipe, c->rast);
231    }
232    c->pipe->delete_sampler_state(c->pipe, c->sampler_linear);
233    c->pipe->delete_sampler_state(c->pipe, c->sampler_nearest);
234 }
235 
236 static bool
init_buffers(struct vl_compositor * c)237 init_buffers(struct vl_compositor *c)
238 {
239    struct pipe_vertex_element vertex_elems[3];
240 
241    assert(c);
242 
243    /*
244     * Create our vertex buffer and vertex buffer elements
245     */
246    c->vertex_buf.stride = sizeof(struct vertex2f) + sizeof(struct vertex4f) * 2;
247    c->vertex_buf.buffer_offset = 0;
248    c->vertex_buf.buffer.resource = NULL;
249    c->vertex_buf.is_user_buffer = false;
250 
251    if (c->pipe_gfx_supported) {
252            vertex_elems[0].src_offset = 0;
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].instance_divisor = 0;
258            vertex_elems[1].vertex_buffer_index = 0;
259            vertex_elems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
260            vertex_elems[2].src_offset = sizeof(struct vertex2f) + sizeof(struct vertex4f);
261            vertex_elems[2].instance_divisor = 0;
262            vertex_elems[2].vertex_buffer_index = 0;
263            vertex_elems[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
264            c->vertex_elems_state = c->pipe->create_vertex_elements_state(c->pipe, 3, vertex_elems);
265    }
266 
267    return true;
268 }
269 
270 static void
cleanup_buffers(struct vl_compositor * c)271 cleanup_buffers(struct vl_compositor *c)
272 {
273    assert(c);
274 
275    if (c->pipe_gfx_supported) {
276            c->pipe->delete_vertex_elements_state(c->pipe, c->vertex_elems_state);
277    }
278    pipe_resource_reference(&c->vertex_buf.buffer.resource, NULL);
279 }
280 
281 static inline struct u_rect
default_rect(struct vl_compositor_layer * layer)282 default_rect(struct vl_compositor_layer *layer)
283 {
284    struct pipe_resource *res = layer->sampler_views[0]->texture;
285    struct u_rect rect = { 0, res->width0, 0, res->height0 * res->array_size };
286    return rect;
287 }
288 
289 static inline struct vertex2f
calc_topleft(struct vertex2f size,struct u_rect rect)290 calc_topleft(struct vertex2f size, struct u_rect rect)
291 {
292    struct vertex2f res = { rect.x0 / size.x, rect.y0 / size.y };
293    return res;
294 }
295 
296 static inline struct vertex2f
calc_bottomright(struct vertex2f size,struct u_rect rect)297 calc_bottomright(struct vertex2f size, struct u_rect rect)
298 {
299    struct vertex2f res = { rect.x1 / size.x, rect.y1 / size.y };
300    return res;
301 }
302 
303 static inline void
calc_src_and_dst(struct vl_compositor_layer * layer,unsigned width,unsigned height,struct u_rect src,struct u_rect dst)304 calc_src_and_dst(struct vl_compositor_layer *layer, unsigned width, unsigned height,
305                  struct u_rect src, struct u_rect dst)
306 {
307    struct vertex2f size =  { width, height };
308 
309    layer->src.tl = calc_topleft(size, src);
310    layer->src.br = calc_bottomright(size, src);
311    layer->dst.tl = calc_topleft(size, dst);
312    layer->dst.br = calc_bottomright(size, dst);
313    layer->zw.x = 0.0f;
314    layer->zw.y = size.y;
315 }
316 
317 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)318 set_yuv_layer(struct vl_compositor_state *s, struct vl_compositor *c,
319               unsigned layer, struct pipe_video_buffer *buffer,
320               struct u_rect *src_rect, struct u_rect *dst_rect,
321               bool y, enum vl_compositor_deinterlace deinterlace)
322 {
323    struct pipe_sampler_view **sampler_views;
324    float half_a_line;
325    unsigned i;
326 
327    assert(s && c && buffer);
328 
329    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
330 
331    s->interlaced = buffer->interlaced;
332    s->used_layers |= 1 << layer;
333    sampler_views = buffer->get_sampler_view_components(buffer);
334    for (i = 0; i < 3; ++i) {
335       s->layers[layer].samplers[i] = c->sampler_linear;
336       pipe_sampler_view_reference(&s->layers[layer].sampler_views[i], sampler_views[i]);
337    }
338 
339    calc_src_and_dst(&s->layers[layer], buffer->width, buffer->height,
340                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
341                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
342 
343    half_a_line = 0.5f / s->layers[layer].zw.y;
344 
345    switch(deinterlace) {
346    case VL_COMPOSITOR_BOB_TOP:
347       s->layers[layer].zw.x = 0.0f;
348       s->layers[layer].src.tl.y += half_a_line;
349       s->layers[layer].src.br.y += half_a_line;
350       if (c->pipe_gfx_supported)
351           s->layers[layer].fs = (y) ? c->fs_yuv.bob.y : c->fs_yuv.bob.uv;
352       if (c->pipe_cs_composit_supported)
353           s->layers[layer].cs = (y) ? c->cs_yuv.bob.y : c->cs_yuv.bob.uv;
354       break;
355 
356    case VL_COMPOSITOR_BOB_BOTTOM:
357       s->layers[layer].zw.x = 1.0f;
358       s->layers[layer].src.tl.y -= half_a_line;
359       s->layers[layer].src.br.y -= half_a_line;
360       if (c->pipe_gfx_supported)
361           s->layers[layer].fs = (y) ? c->fs_yuv.bob.y : c->fs_yuv.bob.uv;
362       if (c->pipe_cs_composit_supported)
363           s->layers[layer].cs = (y) ? c->cs_yuv.bob.y : c->cs_yuv.bob.uv;
364       break;
365 
366    default:
367       if (c->pipe_gfx_supported)
368           s->layers[layer].fs = (y) ? c->fs_yuv.weave.y : c->fs_yuv.weave.uv;
369       if (c->pipe_cs_composit_supported)
370           s->layers[layer].cs = (y) ? c->cs_yuv.weave.y : c->cs_yuv.weave.uv;
371       break;
372    }
373 }
374 
375 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)376 set_rgb_to_yuv_layer(struct vl_compositor_state *s, struct vl_compositor *c,
377                      unsigned layer, struct pipe_sampler_view *v,
378                      struct u_rect *src_rect, struct u_rect *dst_rect, bool y)
379 {
380    vl_csc_matrix csc_matrix;
381 
382    assert(s && c && v);
383 
384    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
385 
386    s->used_layers |= 1 << layer;
387 
388    s->layers[layer].fs = y? c->fs_rgb_yuv.y : c->fs_rgb_yuv.uv;
389 
390    vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_709_REV, NULL, false, &csc_matrix);
391    vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix, 1.0f, 0.0f);
392 
393    s->layers[layer].samplers[0] = c->sampler_linear;
394    s->layers[layer].samplers[1] = NULL;
395    s->layers[layer].samplers[2] = NULL;
396 
397    pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], v);
398    pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], NULL);
399    pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);
400 
401    calc_src_and_dst(&s->layers[layer], v->texture->width0, v->texture->height0,
402                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
403                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
404 }
405 
406 void
vl_compositor_reset_dirty_area(struct u_rect * dirty)407 vl_compositor_reset_dirty_area(struct u_rect *dirty)
408 {
409    assert(dirty);
410 
411    dirty->x0 = dirty->y0 = VL_COMPOSITOR_MIN_DIRTY;
412    dirty->x1 = dirty->y1 = VL_COMPOSITOR_MAX_DIRTY;
413 }
414 
415 void
vl_compositor_set_clear_color(struct vl_compositor_state * s,union pipe_color_union * color)416 vl_compositor_set_clear_color(struct vl_compositor_state *s, union pipe_color_union *color)
417 {
418    assert(s);
419    assert(color);
420 
421    s->clear_color = *color;
422 }
423 
424 void
vl_compositor_get_clear_color(struct vl_compositor_state * s,union pipe_color_union * color)425 vl_compositor_get_clear_color(struct vl_compositor_state *s, union pipe_color_union *color)
426 {
427    assert(s);
428    assert(color);
429 
430    *color = s->clear_color;
431 }
432 
433 void
vl_compositor_clear_layers(struct vl_compositor_state * s)434 vl_compositor_clear_layers(struct vl_compositor_state *s)
435 {
436    unsigned i, j;
437 
438    assert(s);
439    s->interlaced = false;
440    s->used_layers = 0;
441    for ( i = 0; i < VL_COMPOSITOR_MAX_LAYERS; ++i) {
442       struct vertex4f v_one = { 1.0f, 1.0f, 1.0f, 1.0f };
443       s->layers[i].clearing = i ? false : true;
444       s->layers[i].blend = NULL;
445       s->layers[i].fs = NULL;
446       s->layers[i].cs = NULL;
447       s->layers[i].viewport.scale[2] = 1;
448       s->layers[i].viewport.translate[2] = 0;
449       s->layers[i].rotate = VL_COMPOSITOR_ROTATE_0;
450 
451       for ( j = 0; j < 3; j++)
452          pipe_sampler_view_reference(&s->layers[i].sampler_views[j], NULL);
453       for ( j = 0; j < 4; ++j)
454          s->layers[i].colors[j] = v_one;
455    }
456 }
457 
458 void
vl_compositor_cleanup(struct vl_compositor * c)459 vl_compositor_cleanup(struct vl_compositor *c)
460 {
461    assert(c);
462 
463    cleanup_buffers(c);
464    cleanup_shaders(c);
465    cleanup_pipe_state(c);
466 }
467 
468 bool
vl_compositor_set_csc_matrix(struct vl_compositor_state * s,vl_csc_matrix const * matrix,float luma_min,float luma_max)469 vl_compositor_set_csc_matrix(struct vl_compositor_state *s,
470                              vl_csc_matrix const *matrix,
471                              float luma_min, float luma_max)
472 {
473    struct pipe_transfer *buf_transfer;
474 
475    assert(s);
476 
477    float *ptr = pipe_buffer_map(s->pipe, s->shader_params,
478                                PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE,
479                                &buf_transfer);
480 
481    if (!ptr)
482       return false;
483 
484    memcpy(ptr, matrix, sizeof(vl_csc_matrix));
485 
486    ptr += sizeof(vl_csc_matrix)/sizeof(float);
487    ptr[0] = luma_min;
488    ptr[1] = luma_max;
489 
490    pipe_buffer_unmap(s->pipe, buf_transfer);
491 
492    return true;
493 }
494 
495 void
vl_compositor_set_dst_clip(struct vl_compositor_state * s,struct u_rect * dst_clip)496 vl_compositor_set_dst_clip(struct vl_compositor_state *s, struct u_rect *dst_clip)
497 {
498    assert(s);
499 
500    s->scissor_valid = dst_clip != NULL;
501    if (dst_clip) {
502       s->scissor.minx = dst_clip->x0;
503       s->scissor.miny = dst_clip->y0;
504       s->scissor.maxx = dst_clip->x1;
505       s->scissor.maxy = dst_clip->y1;
506    }
507 }
508 
509 void
vl_compositor_set_layer_blend(struct vl_compositor_state * s,unsigned layer,void * blend,bool is_clearing)510 vl_compositor_set_layer_blend(struct vl_compositor_state *s,
511                               unsigned layer, void *blend,
512                               bool is_clearing)
513 {
514    assert(s && blend);
515 
516    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
517 
518    s->layers[layer].clearing = is_clearing;
519    s->layers[layer].blend = blend;
520 }
521 
522 void
vl_compositor_set_layer_dst_area(struct vl_compositor_state * s,unsigned layer,struct u_rect * dst_area)523 vl_compositor_set_layer_dst_area(struct vl_compositor_state *s,
524                                  unsigned layer, struct u_rect *dst_area)
525 {
526    assert(s);
527 
528    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
529 
530    s->layers[layer].viewport_valid = dst_area != NULL;
531    if (dst_area) {
532       s->layers[layer].viewport.scale[0] = dst_area->x1 - dst_area->x0;
533       s->layers[layer].viewport.scale[1] = dst_area->y1 - dst_area->y0;
534       s->layers[layer].viewport.translate[0] = dst_area->x0;
535       s->layers[layer].viewport.translate[1] = dst_area->y0;
536    }
537 }
538 
539 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)540 vl_compositor_set_buffer_layer(struct vl_compositor_state *s,
541                                struct vl_compositor *c,
542                                unsigned layer,
543                                struct pipe_video_buffer *buffer,
544                                struct u_rect *src_rect,
545                                struct u_rect *dst_rect,
546                                enum vl_compositor_deinterlace deinterlace)
547 {
548    struct pipe_sampler_view **sampler_views;
549    unsigned i;
550 
551    assert(s && c && buffer);
552 
553    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
554 
555    s->interlaced = buffer->interlaced;
556    s->used_layers |= 1 << layer;
557    sampler_views = buffer->get_sampler_view_components(buffer);
558    for (i = 0; i < 3; ++i) {
559       s->layers[layer].samplers[i] = c->sampler_linear;
560       pipe_sampler_view_reference(&s->layers[layer].sampler_views[i], sampler_views[i]);
561    }
562 
563    calc_src_and_dst(&s->layers[layer], buffer->width, buffer->height,
564                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
565                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
566 
567    if (buffer->interlaced) {
568       float half_a_line = 0.5f / s->layers[layer].zw.y;
569       switch(deinterlace) {
570       case VL_COMPOSITOR_WEAVE:
571          if (c->pipe_cs_composit_supported)
572             s->layers[layer].cs = c->cs_weave_rgb;
573          else if (c->pipe_gfx_supported)
574             s->layers[layer].fs = c->fs_weave_rgb;
575          break;
576 
577       case VL_COMPOSITOR_BOB_TOP:
578          s->layers[layer].zw.x = 0.0f;
579          s->layers[layer].src.tl.y += half_a_line;
580          s->layers[layer].src.br.y += half_a_line;
581          if (c->pipe_cs_composit_supported)
582             s->layers[layer].cs = c->cs_video_buffer;
583          else if (c->pipe_gfx_supported)
584             s->layers[layer].fs = c->fs_video_buffer;
585          break;
586 
587       case VL_COMPOSITOR_BOB_BOTTOM:
588          s->layers[layer].zw.x = 1.0f;
589          s->layers[layer].src.tl.y -= half_a_line;
590          s->layers[layer].src.br.y -= half_a_line;
591          if (c->pipe_cs_composit_supported)
592             s->layers[layer].cs = c->cs_video_buffer;
593          else if (c->pipe_gfx_supported)
594             s->layers[layer].fs = c->fs_video_buffer;
595          break;
596       }
597 
598    } else {
599       if (c->pipe_cs_composit_supported)
600          s->layers[layer].cs = c->cs_video_buffer;
601       else if (c->pipe_gfx_supported)
602          s->layers[layer].fs = c->fs_video_buffer;
603    }
604 }
605 
606 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)607 vl_compositor_set_palette_layer(struct vl_compositor_state *s,
608                                 struct vl_compositor *c,
609                                 unsigned layer,
610                                 struct pipe_sampler_view *indexes,
611                                 struct pipe_sampler_view *palette,
612                                 struct u_rect *src_rect,
613                                 struct u_rect *dst_rect,
614                                 bool include_color_conversion)
615 {
616    assert(s && c && indexes && palette);
617 
618    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
619 
620    s->used_layers |= 1 << layer;
621 
622    s->layers[layer].fs = include_color_conversion ?
623       c->fs_palette.yuv : c->fs_palette.rgb;
624 
625    s->layers[layer].samplers[0] = c->sampler_linear;
626    s->layers[layer].samplers[1] = c->sampler_nearest;
627    s->layers[layer].samplers[2] = NULL;
628    pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], indexes);
629    pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], palette);
630    pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);
631    calc_src_and_dst(&s->layers[layer], indexes->texture->width0, indexes->texture->height0,
632                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
633                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
634 }
635 
636 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)637 vl_compositor_set_rgba_layer(struct vl_compositor_state *s,
638                              struct vl_compositor *c,
639                              unsigned layer,
640                              struct pipe_sampler_view *rgba,
641                              struct u_rect *src_rect,
642                              struct u_rect *dst_rect,
643                              struct vertex4f *colors)
644 {
645    unsigned i;
646 
647    assert(s && c && rgba);
648 
649    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
650 
651    s->used_layers |= 1 << layer;
652    s->layers[layer].fs = c->fs_rgba;
653    s->layers[layer].samplers[0] = c->sampler_linear;
654    s->layers[layer].samplers[1] = NULL;
655    s->layers[layer].samplers[2] = NULL;
656    pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], rgba);
657    pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], NULL);
658    pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);
659    calc_src_and_dst(&s->layers[layer], rgba->texture->width0, rgba->texture->height0,
660                     src_rect ? *src_rect : default_rect(&s->layers[layer]),
661                     dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
662 
663    if (colors)
664       for (i = 0; i < 4; ++i)
665          s->layers[layer].colors[i] = colors[i];
666 }
667 
668 void
vl_compositor_set_layer_rotation(struct vl_compositor_state * s,unsigned layer,enum vl_compositor_rotation rotate)669 vl_compositor_set_layer_rotation(struct vl_compositor_state *s,
670                                  unsigned layer,
671                                  enum vl_compositor_rotation rotate)
672 {
673    assert(s);
674    assert(layer < VL_COMPOSITOR_MAX_LAYERS);
675    s->layers[layer].rotate = rotate;
676 }
677 
678 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)679 vl_compositor_yuv_deint_full(struct vl_compositor_state *s,
680                              struct vl_compositor *c,
681                              struct pipe_video_buffer *src,
682                              struct pipe_video_buffer *dst,
683                              struct u_rect *src_rect,
684                              struct u_rect *dst_rect,
685                              enum vl_compositor_deinterlace deinterlace)
686 {
687    struct pipe_surface **dst_surfaces;
688 
689    dst_surfaces = dst->get_surfaces(dst);
690    vl_compositor_clear_layers(s);
691 
692    set_yuv_layer(s, c, 0, src, src_rect, NULL, true, deinterlace);
693    vl_compositor_set_layer_dst_area(s, 0, dst_rect);
694    vl_compositor_render(s, c, dst_surfaces[0], NULL, false);
695 
696    if (dst_rect) {
697       dst_rect->x1 /= 2;
698       dst_rect->y1 /= 2;
699    }
700 
701    set_yuv_layer(s, c, 0, src, src_rect, NULL, false, deinterlace);
702    vl_compositor_set_layer_dst_area(s, 0, dst_rect);
703    vl_compositor_render(s, c, dst_surfaces[1], NULL, false);
704 
705    s->pipe->flush(s->pipe, NULL, 0);
706 }
707 
708 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)709 vl_compositor_convert_rgb_to_yuv(struct vl_compositor_state *s,
710                                  struct vl_compositor *c,
711                                  unsigned layer,
712                                  struct pipe_resource *src_res,
713                                  struct pipe_video_buffer *dst,
714                                  struct u_rect *src_rect,
715                                  struct u_rect *dst_rect)
716 {
717    struct pipe_sampler_view *sv, sv_templ;
718    struct pipe_surface **dst_surfaces;
719 
720    dst_surfaces = dst->get_surfaces(dst);
721 
722    memset(&sv_templ, 0, sizeof(sv_templ));
723    u_sampler_view_default_template(&sv_templ, src_res, src_res->format);
724    sv = s->pipe->create_sampler_view(s->pipe, src_res, &sv_templ);
725 
726    vl_compositor_clear_layers(s);
727 
728    set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, true);
729    vl_compositor_set_layer_dst_area(s, 0, dst_rect);
730    vl_compositor_render(s, c, dst_surfaces[0], NULL, false);
731 
732    if (dst_rect) {
733       dst_rect->x1 /= 2;
734       dst_rect->y1 /= 2;
735    }
736 
737    set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, false);
738    vl_compositor_set_layer_dst_area(s, 0, dst_rect);
739    vl_compositor_render(s, c, dst_surfaces[1], NULL, false);
740    pipe_sampler_view_reference(&sv, NULL);
741 
742    s->pipe->flush(s->pipe, NULL, 0);
743 }
744 
745 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)746 vl_compositor_render(struct vl_compositor_state *s,
747                      struct vl_compositor       *c,
748                      struct pipe_surface        *dst_surface,
749                      struct u_rect              *dirty_area,
750                      bool                        clear_dirty)
751 {
752    assert(s);
753 
754    if (s->layers->cs)
755       vl_compositor_cs_render(s, c, dst_surface, dirty_area, clear_dirty);
756    else if (s->layers->fs)
757       vl_compositor_gfx_render(s, c, dst_surface, dirty_area, clear_dirty);
758    else
759       debug_warning("Hardware don't support.\n");;
760 }
761 
762 bool
vl_compositor_init(struct vl_compositor * c,struct pipe_context * pipe)763 vl_compositor_init(struct vl_compositor *c, struct pipe_context *pipe)
764 {
765    assert(c);
766 
767    memset(c, 0, sizeof(*c));
768 
769    c->pipe_cs_composit_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_PREFER_COMPUTE_FOR_MULTIMEDIA) &&
770             pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_TEX_TXF_LZ) &&
771             pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_DIV);
772 
773    c->pipe_gfx_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_GRAPHICS);
774    c->pipe = pipe;
775 
776    if (!init_pipe_state(c)) {
777       return false;
778    }
779 
780    if (!init_shaders(c)) {
781       cleanup_pipe_state(c);
782       return false;
783    }
784 
785    if (!init_buffers(c)) {
786       cleanup_shaders(c);
787       cleanup_pipe_state(c);
788       return false;
789    }
790 
791    return true;
792 }
793 
794 bool
vl_compositor_init_state(struct vl_compositor_state * s,struct pipe_context * pipe)795 vl_compositor_init_state(struct vl_compositor_state *s, struct pipe_context *pipe)
796 {
797    vl_csc_matrix csc_matrix;
798 
799    assert(s);
800 
801    memset(s, 0, sizeof(*s));
802 
803    s->pipe = pipe;
804 
805    s->clear_color.f[0] = s->clear_color.f[1] = 0.0f;
806    s->clear_color.f[2] = s->clear_color.f[3] = 0.0f;
807 
808    /*
809     * Create our fragment shader's constant buffer
810     * Const buffer contains the color conversion matrix and bias vectors
811     */
812    /* XXX: Create with IMMUTABLE/STATIC... although it does change every once in a long while... */
813    s->shader_params = pipe_buffer_create_const0
814    (
815       pipe->screen,
816       PIPE_BIND_CONSTANT_BUFFER,
817       PIPE_USAGE_DEFAULT,
818       sizeof(csc_matrix) + 6*sizeof(float) + 10*sizeof(int)
819    );
820 
821    if (!s->shader_params)
822       return false;
823 
824    vl_compositor_clear_layers(s);
825 
826    vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, &csc_matrix);
827    if (!vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix, 1.0f, 0.0f))
828       return false;
829 
830    return true;
831 }
832 
833 void
vl_compositor_cleanup_state(struct vl_compositor_state * s)834 vl_compositor_cleanup_state(struct vl_compositor_state *s)
835 {
836    assert(s);
837 
838    vl_compositor_clear_layers(s);
839    pipe_resource_reference(&s->shader_params, NULL);
840 }
841