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 33static bool 34init_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 102static 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 127static bool 128init_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 218static 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 236static bool 237init_buffers(struct vl_compositor *c) 238{ 239 struct pipe_vertex_element vertex_elems[3]; 240 memset(vertex_elems, 0, sizeof(vertex_elems)); 241 242 assert(c); 243 244 /* 245 * Create our vertex buffer and vertex buffer elements 246 */ 247 c->vertex_buf.stride = sizeof(struct vertex2f) + sizeof(struct vertex4f) * 2; 248 c->vertex_buf.buffer_offset = 0; 249 c->vertex_buf.buffer.resource = NULL; 250 c->vertex_buf.is_user_buffer = false; 251 252 if (c->pipe_gfx_supported) { 253 vertex_elems[0].src_offset = 0; 254 vertex_elems[0].instance_divisor = 0; 255 vertex_elems[0].vertex_buffer_index = 0; 256 vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT; 257 vertex_elems[1].src_offset = sizeof(struct vertex2f); 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[2].instance_divisor = 0; 263 vertex_elems[2].vertex_buffer_index = 0; 264 vertex_elems[2].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 265 c->vertex_elems_state = c->pipe->create_vertex_elements_state(c->pipe, 3, vertex_elems); 266 } 267 268 return true; 269} 270 271static void 272cleanup_buffers(struct vl_compositor *c) 273{ 274 assert(c); 275 276 if (c->pipe_gfx_supported) { 277 c->pipe->delete_vertex_elements_state(c->pipe, c->vertex_elems_state); 278 } 279 pipe_resource_reference(&c->vertex_buf.buffer.resource, NULL); 280} 281 282static inline struct u_rect 283default_rect(struct vl_compositor_layer *layer) 284{ 285 struct pipe_resource *res = layer->sampler_views[0]->texture; 286 struct u_rect rect = { 0, res->width0, 0, res->height0 * res->array_size }; 287 return rect; 288} 289 290static inline struct vertex2f 291calc_topleft(struct vertex2f size, struct u_rect rect) 292{ 293 struct vertex2f res = { rect.x0 / size.x, rect.y0 / size.y }; 294 return res; 295} 296 297static inline struct vertex2f 298calc_bottomright(struct vertex2f size, struct u_rect rect) 299{ 300 struct vertex2f res = { rect.x1 / size.x, rect.y1 / size.y }; 301 return res; 302} 303 304static inline void 305calc_src_and_dst(struct vl_compositor_layer *layer, unsigned width, unsigned height, 306 struct u_rect src, struct u_rect dst) 307{ 308 struct vertex2f size = { width, height }; 309 310 layer->src.tl = calc_topleft(size, src); 311 layer->src.br = calc_bottomright(size, src); 312 layer->dst.tl = calc_topleft(size, dst); 313 layer->dst.br = calc_bottomright(size, dst); 314 layer->zw.x = 0.0f; 315 layer->zw.y = size.y; 316} 317 318static void 319set_yuv_layer(struct vl_compositor_state *s, struct vl_compositor *c, 320 unsigned layer, struct pipe_video_buffer *buffer, 321 struct u_rect *src_rect, struct u_rect *dst_rect, 322 bool y, enum vl_compositor_deinterlace deinterlace) 323{ 324 struct pipe_sampler_view **sampler_views; 325 float half_a_line; 326 unsigned i; 327 328 assert(s && c && buffer); 329 330 assert(layer < VL_COMPOSITOR_MAX_LAYERS); 331 332 s->interlaced = buffer->interlaced; 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.bob.y : c->cs_yuv.bob.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.bob.y : c->cs_yuv.bob.uv; 365 break; 366 367 default: 368 if (c->pipe_gfx_supported) 369 s->layers[layer].fs = (y) ? c->fs_yuv.weave.y : c->fs_yuv.weave.uv; 370 if (c->pipe_cs_composit_supported) 371 s->layers[layer].cs = (y) ? c->cs_yuv.weave.y : c->cs_yuv.weave.uv; 372 break; 373 } 374} 375 376static void 377set_rgb_to_yuv_layer(struct vl_compositor_state *s, struct vl_compositor *c, 378 unsigned layer, struct pipe_sampler_view *v, 379 struct u_rect *src_rect, struct u_rect *dst_rect, bool y) 380{ 381 vl_csc_matrix csc_matrix; 382 383 assert(s && c && v); 384 385 assert(layer < VL_COMPOSITOR_MAX_LAYERS); 386 387 s->used_layers |= 1 << layer; 388 389 s->layers[layer].fs = y? c->fs_rgb_yuv.y : c->fs_rgb_yuv.uv; 390 391 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_BT_709_REV, NULL, false, &csc_matrix); 392 vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix, 1.0f, 0.0f); 393 394 s->layers[layer].samplers[0] = c->sampler_linear; 395 s->layers[layer].samplers[1] = NULL; 396 s->layers[layer].samplers[2] = NULL; 397 398 pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], v); 399 pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], NULL); 400 pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL); 401 402 calc_src_and_dst(&s->layers[layer], v->texture->width0, v->texture->height0, 403 src_rect ? *src_rect : default_rect(&s->layers[layer]), 404 dst_rect ? *dst_rect : default_rect(&s->layers[layer])); 405} 406 407void 408vl_compositor_reset_dirty_area(struct u_rect *dirty) 409{ 410 assert(dirty); 411 412 dirty->x0 = dirty->y0 = VL_COMPOSITOR_MIN_DIRTY; 413 dirty->x1 = dirty->y1 = VL_COMPOSITOR_MAX_DIRTY; 414} 415 416void 417vl_compositor_set_clear_color(struct vl_compositor_state *s, union pipe_color_union *color) 418{ 419 assert(s); 420 assert(color); 421 422 s->clear_color = *color; 423} 424 425void 426vl_compositor_get_clear_color(struct vl_compositor_state *s, union pipe_color_union *color) 427{ 428 assert(s); 429 assert(color); 430 431 *color = s->clear_color; 432} 433 434void 435vl_compositor_clear_layers(struct vl_compositor_state *s) 436{ 437 unsigned i, j; 438 439 assert(s); 440 s->interlaced = false; 441 s->used_layers = 0; 442 for ( i = 0; i < VL_COMPOSITOR_MAX_LAYERS; ++i) { 443 struct vertex4f v_one = { 1.0f, 1.0f, 1.0f, 1.0f }; 444 s->layers[i].clearing = i ? false : true; 445 s->layers[i].blend = NULL; 446 s->layers[i].fs = NULL; 447 s->layers[i].cs = NULL; 448 s->layers[i].viewport.scale[2] = 1; 449 s->layers[i].viewport.translate[2] = 0; 450 s->layers[i].viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X; 451 s->layers[i].viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y; 452 s->layers[i].viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z; 453 s->layers[i].viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W; 454 s->layers[i].rotate = VL_COMPOSITOR_ROTATE_0; 455 456 for ( j = 0; j < 3; j++) 457 pipe_sampler_view_reference(&s->layers[i].sampler_views[j], NULL); 458 for ( j = 0; j < 4; ++j) 459 s->layers[i].colors[j] = v_one; 460 } 461} 462 463void 464vl_compositor_cleanup(struct vl_compositor *c) 465{ 466 assert(c); 467 468 cleanup_buffers(c); 469 cleanup_shaders(c); 470 cleanup_pipe_state(c); 471} 472 473bool 474vl_compositor_set_csc_matrix(struct vl_compositor_state *s, 475 vl_csc_matrix const *matrix, 476 float luma_min, float luma_max) 477{ 478 struct pipe_transfer *buf_transfer; 479 480 assert(s); 481 482 float *ptr = pipe_buffer_map(s->pipe, s->shader_params, 483 PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE, 484 &buf_transfer); 485 486 if (!ptr) 487 return false; 488 489 memcpy(ptr, matrix, sizeof(vl_csc_matrix)); 490 491 ptr += sizeof(vl_csc_matrix)/sizeof(float); 492 ptr[0] = luma_min; 493 ptr[1] = luma_max; 494 495 pipe_buffer_unmap(s->pipe, buf_transfer); 496 497 return true; 498} 499 500void 501vl_compositor_set_dst_clip(struct vl_compositor_state *s, struct u_rect *dst_clip) 502{ 503 assert(s); 504 505 s->scissor_valid = dst_clip != NULL; 506 if (dst_clip) { 507 s->scissor.minx = dst_clip->x0; 508 s->scissor.miny = dst_clip->y0; 509 s->scissor.maxx = dst_clip->x1; 510 s->scissor.maxy = dst_clip->y1; 511 } 512} 513 514void 515vl_compositor_set_layer_blend(struct vl_compositor_state *s, 516 unsigned layer, void *blend, 517 bool is_clearing) 518{ 519 assert(s && blend); 520 521 assert(layer < VL_COMPOSITOR_MAX_LAYERS); 522 523 s->layers[layer].clearing = is_clearing; 524 s->layers[layer].blend = blend; 525} 526 527void 528vl_compositor_set_layer_dst_area(struct vl_compositor_state *s, 529 unsigned layer, struct u_rect *dst_area) 530{ 531 assert(s); 532 533 assert(layer < VL_COMPOSITOR_MAX_LAYERS); 534 535 s->layers[layer].viewport_valid = dst_area != NULL; 536 if (dst_area) { 537 s->layers[layer].viewport.scale[0] = dst_area->x1 - dst_area->x0; 538 s->layers[layer].viewport.scale[1] = dst_area->y1 - dst_area->y0; 539 s->layers[layer].viewport.translate[0] = dst_area->x0; 540 s->layers[layer].viewport.translate[1] = dst_area->y0; 541 } 542} 543 544void 545vl_compositor_set_buffer_layer(struct vl_compositor_state *s, 546 struct vl_compositor *c, 547 unsigned layer, 548 struct pipe_video_buffer *buffer, 549 struct u_rect *src_rect, 550 struct u_rect *dst_rect, 551 enum vl_compositor_deinterlace deinterlace) 552{ 553 struct pipe_sampler_view **sampler_views; 554 unsigned i; 555 556 assert(s && c && buffer); 557 558 assert(layer < VL_COMPOSITOR_MAX_LAYERS); 559 560 s->interlaced = buffer->interlaced; 561 s->used_layers |= 1 << layer; 562 sampler_views = buffer->get_sampler_view_components(buffer); 563 for (i = 0; i < 3; ++i) { 564 s->layers[layer].samplers[i] = c->sampler_linear; 565 pipe_sampler_view_reference(&s->layers[layer].sampler_views[i], sampler_views[i]); 566 } 567 568 calc_src_and_dst(&s->layers[layer], buffer->width, buffer->height, 569 src_rect ? *src_rect : default_rect(&s->layers[layer]), 570 dst_rect ? *dst_rect : default_rect(&s->layers[layer])); 571 572 if (buffer->interlaced) { 573 float half_a_line = 0.5f / s->layers[layer].zw.y; 574 switch(deinterlace) { 575 case VL_COMPOSITOR_NONE: 576 case VL_COMPOSITOR_MOTION_ADAPTIVE: 577 case VL_COMPOSITOR_WEAVE: 578 if (c->pipe_cs_composit_supported) 579 s->layers[layer].cs = c->cs_weave_rgb; 580 else if (c->pipe_gfx_supported) 581 s->layers[layer].fs = c->fs_weave_rgb; 582 break; 583 584 case VL_COMPOSITOR_BOB_TOP: 585 s->layers[layer].zw.x = 0.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 case VL_COMPOSITOR_BOB_BOTTOM: 595 s->layers[layer].zw.x = 1.0f; 596 s->layers[layer].src.tl.y -= half_a_line; 597 s->layers[layer].src.br.y -= half_a_line; 598 if (c->pipe_cs_composit_supported) 599 s->layers[layer].cs = c->cs_video_buffer; 600 else if (c->pipe_gfx_supported) 601 s->layers[layer].fs = c->fs_video_buffer; 602 break; 603 } 604 605 } else { 606 if (c->pipe_cs_composit_supported) 607 s->layers[layer].cs = c->cs_video_buffer; 608 else if (c->pipe_gfx_supported) 609 s->layers[layer].fs = c->fs_video_buffer; 610 } 611} 612 613void 614vl_compositor_set_palette_layer(struct vl_compositor_state *s, 615 struct vl_compositor *c, 616 unsigned layer, 617 struct pipe_sampler_view *indexes, 618 struct pipe_sampler_view *palette, 619 struct u_rect *src_rect, 620 struct u_rect *dst_rect, 621 bool include_color_conversion) 622{ 623 assert(s && c && indexes && palette); 624 625 assert(layer < VL_COMPOSITOR_MAX_LAYERS); 626 627 s->used_layers |= 1 << layer; 628 629 s->layers[layer].fs = include_color_conversion ? 630 c->fs_palette.yuv : c->fs_palette.rgb; 631 632 s->layers[layer].samplers[0] = c->sampler_linear; 633 s->layers[layer].samplers[1] = c->sampler_nearest; 634 s->layers[layer].samplers[2] = NULL; 635 pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], indexes); 636 pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], palette); 637 pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL); 638 calc_src_and_dst(&s->layers[layer], indexes->texture->width0, indexes->texture->height0, 639 src_rect ? *src_rect : default_rect(&s->layers[layer]), 640 dst_rect ? *dst_rect : default_rect(&s->layers[layer])); 641} 642 643void 644vl_compositor_set_rgba_layer(struct vl_compositor_state *s, 645 struct vl_compositor *c, 646 unsigned layer, 647 struct pipe_sampler_view *rgba, 648 struct u_rect *src_rect, 649 struct u_rect *dst_rect, 650 struct vertex4f *colors) 651{ 652 unsigned i; 653 654 assert(s && c && rgba); 655 656 assert(layer < VL_COMPOSITOR_MAX_LAYERS); 657 658 s->used_layers |= 1 << layer; 659 s->layers[layer].fs = c->fs_rgba; 660 s->layers[layer].samplers[0] = c->sampler_linear; 661 s->layers[layer].samplers[1] = NULL; 662 s->layers[layer].samplers[2] = NULL; 663 pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], rgba); 664 pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], NULL); 665 pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL); 666 calc_src_and_dst(&s->layers[layer], rgba->texture->width0, rgba->texture->height0, 667 src_rect ? *src_rect : default_rect(&s->layers[layer]), 668 dst_rect ? *dst_rect : default_rect(&s->layers[layer])); 669 670 if (colors) 671 for (i = 0; i < 4; ++i) 672 s->layers[layer].colors[i] = colors[i]; 673} 674 675void 676vl_compositor_set_layer_rotation(struct vl_compositor_state *s, 677 unsigned layer, 678 enum vl_compositor_rotation rotate) 679{ 680 assert(s); 681 assert(layer < VL_COMPOSITOR_MAX_LAYERS); 682 s->layers[layer].rotate = rotate; 683} 684 685void 686vl_compositor_yuv_deint_full(struct vl_compositor_state *s, 687 struct vl_compositor *c, 688 struct pipe_video_buffer *src, 689 struct pipe_video_buffer *dst, 690 struct u_rect *src_rect, 691 struct u_rect *dst_rect, 692 enum vl_compositor_deinterlace deinterlace) 693{ 694 struct pipe_surface **dst_surfaces; 695 696 dst_surfaces = dst->get_surfaces(dst); 697 vl_compositor_clear_layers(s); 698 699 set_yuv_layer(s, c, 0, src, src_rect, NULL, true, deinterlace); 700 vl_compositor_set_layer_dst_area(s, 0, dst_rect); 701 vl_compositor_render(s, c, dst_surfaces[0], NULL, false); 702 703 if (dst_rect) { 704 dst_rect->x1 /= 2; 705 dst_rect->y1 /= 2; 706 } 707 708 set_yuv_layer(s, c, 0, src, src_rect, NULL, false, deinterlace); 709 vl_compositor_set_layer_dst_area(s, 0, dst_rect); 710 vl_compositor_render(s, c, dst_surfaces[1], NULL, false); 711 712 s->pipe->flush(s->pipe, NULL, 0); 713} 714 715void 716vl_compositor_convert_rgb_to_yuv(struct vl_compositor_state *s, 717 struct vl_compositor *c, 718 unsigned layer, 719 struct pipe_resource *src_res, 720 struct pipe_video_buffer *dst, 721 struct u_rect *src_rect, 722 struct u_rect *dst_rect) 723{ 724 struct pipe_sampler_view *sv, sv_templ; 725 struct pipe_surface **dst_surfaces; 726 727 dst_surfaces = dst->get_surfaces(dst); 728 729 memset(&sv_templ, 0, sizeof(sv_templ)); 730 u_sampler_view_default_template(&sv_templ, src_res, src_res->format); 731 sv = s->pipe->create_sampler_view(s->pipe, src_res, &sv_templ); 732 733 vl_compositor_clear_layers(s); 734 735 set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, true); 736 vl_compositor_set_layer_dst_area(s, 0, dst_rect); 737 vl_compositor_render(s, c, dst_surfaces[0], NULL, false); 738 739 if (dst_rect) { 740 dst_rect->x1 /= 2; 741 dst_rect->y1 /= 2; 742 } 743 744 set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, false); 745 vl_compositor_set_layer_dst_area(s, 0, dst_rect); 746 vl_compositor_render(s, c, dst_surfaces[1], NULL, false); 747 pipe_sampler_view_reference(&sv, NULL); 748 749 s->pipe->flush(s->pipe, NULL, 0); 750} 751 752void 753vl_compositor_render(struct vl_compositor_state *s, 754 struct vl_compositor *c, 755 struct pipe_surface *dst_surface, 756 struct u_rect *dirty_area, 757 bool clear_dirty) 758{ 759 assert(s); 760 761 if (s->layers->cs) 762 vl_compositor_cs_render(s, c, dst_surface, dirty_area, clear_dirty); 763 else if (s->layers->fs) 764 vl_compositor_gfx_render(s, c, dst_surface, dirty_area, clear_dirty); 765 else 766 debug_warning("Hardware don't support.\n");; 767} 768 769bool 770vl_compositor_init(struct vl_compositor *c, struct pipe_context *pipe) 771{ 772 assert(c); 773 774 memset(c, 0, sizeof(*c)); 775 776 c->pipe_cs_composit_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_PREFER_COMPUTE_FOR_MULTIMEDIA) && 777 pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_TEX_TXF_LZ) && 778 pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_DIV); 779 780 c->pipe_gfx_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_GRAPHICS); 781 c->pipe = pipe; 782 783 c->deinterlace = VL_COMPOSITOR_NONE; 784 785 if (!init_pipe_state(c)) { 786 return false; 787 } 788 789 if (!init_shaders(c)) { 790 cleanup_pipe_state(c); 791 return false; 792 } 793 794 if (!init_buffers(c)) { 795 cleanup_shaders(c); 796 cleanup_pipe_state(c); 797 return false; 798 } 799 800 return true; 801} 802 803bool 804vl_compositor_init_state(struct vl_compositor_state *s, struct pipe_context *pipe) 805{ 806 vl_csc_matrix csc_matrix; 807 808 assert(s); 809 810 memset(s, 0, sizeof(*s)); 811 812 s->pipe = pipe; 813 814 s->clear_color.f[0] = s->clear_color.f[1] = 0.0f; 815 s->clear_color.f[2] = s->clear_color.f[3] = 0.0f; 816 817 /* 818 * Create our fragment shader's constant buffer 819 * Const buffer contains the color conversion matrix and bias vectors 820 */ 821 /* XXX: Create with IMMUTABLE/STATIC... although it does change every once in a long while... */ 822 s->shader_params = pipe_buffer_create_const0 823 ( 824 pipe->screen, 825 PIPE_BIND_CONSTANT_BUFFER, 826 PIPE_USAGE_DEFAULT, 827 sizeof(csc_matrix) + 6*sizeof(float) + 10*sizeof(int) 828 ); 829 830 if (!s->shader_params) 831 return false; 832 833 vl_compositor_clear_layers(s); 834 835 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, &csc_matrix); 836 if (!vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix, 1.0f, 0.0f)) 837 return false; 838 839 return true; 840} 841 842void 843vl_compositor_cleanup_state(struct vl_compositor_state *s) 844{ 845 assert(s); 846 847 vl_compositor_clear_layers(s); 848 pipe_resource_reference(&s->shader_params, NULL); 849} 850