Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/auxiliary/vl/vl_compositor.c
4565 views
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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].viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
450
s->layers[i].viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
451
s->layers[i].viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
452
s->layers[i].viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
453
s->layers[i].rotate = VL_COMPOSITOR_ROTATE_0;
454
455
for ( j = 0; j < 3; j++)
456
pipe_sampler_view_reference(&s->layers[i].sampler_views[j], NULL);
457
for ( j = 0; j < 4; ++j)
458
s->layers[i].colors[j] = v_one;
459
}
460
}
461
462
void
463
vl_compositor_cleanup(struct vl_compositor *c)
464
{
465
assert(c);
466
467
cleanup_buffers(c);
468
cleanup_shaders(c);
469
cleanup_pipe_state(c);
470
}
471
472
bool
473
vl_compositor_set_csc_matrix(struct vl_compositor_state *s,
474
vl_csc_matrix const *matrix,
475
float luma_min, float luma_max)
476
{
477
struct pipe_transfer *buf_transfer;
478
479
assert(s);
480
481
float *ptr = pipe_buffer_map(s->pipe, s->shader_params,
482
PIPE_MAP_WRITE | PIPE_MAP_DISCARD_RANGE,
483
&buf_transfer);
484
485
if (!ptr)
486
return false;
487
488
memcpy(ptr, matrix, sizeof(vl_csc_matrix));
489
490
ptr += sizeof(vl_csc_matrix)/sizeof(float);
491
ptr[0] = luma_min;
492
ptr[1] = luma_max;
493
494
pipe_buffer_unmap(s->pipe, buf_transfer);
495
496
return true;
497
}
498
499
void
500
vl_compositor_set_dst_clip(struct vl_compositor_state *s, struct u_rect *dst_clip)
501
{
502
assert(s);
503
504
s->scissor_valid = dst_clip != NULL;
505
if (dst_clip) {
506
s->scissor.minx = dst_clip->x0;
507
s->scissor.miny = dst_clip->y0;
508
s->scissor.maxx = dst_clip->x1;
509
s->scissor.maxy = dst_clip->y1;
510
}
511
}
512
513
void
514
vl_compositor_set_layer_blend(struct vl_compositor_state *s,
515
unsigned layer, void *blend,
516
bool is_clearing)
517
{
518
assert(s && blend);
519
520
assert(layer < VL_COMPOSITOR_MAX_LAYERS);
521
522
s->layers[layer].clearing = is_clearing;
523
s->layers[layer].blend = blend;
524
}
525
526
void
527
vl_compositor_set_layer_dst_area(struct vl_compositor_state *s,
528
unsigned layer, struct u_rect *dst_area)
529
{
530
assert(s);
531
532
assert(layer < VL_COMPOSITOR_MAX_LAYERS);
533
534
s->layers[layer].viewport_valid = dst_area != NULL;
535
if (dst_area) {
536
s->layers[layer].viewport.scale[0] = dst_area->x1 - dst_area->x0;
537
s->layers[layer].viewport.scale[1] = dst_area->y1 - dst_area->y0;
538
s->layers[layer].viewport.translate[0] = dst_area->x0;
539
s->layers[layer].viewport.translate[1] = dst_area->y0;
540
}
541
}
542
543
void
544
vl_compositor_set_buffer_layer(struct vl_compositor_state *s,
545
struct vl_compositor *c,
546
unsigned layer,
547
struct pipe_video_buffer *buffer,
548
struct u_rect *src_rect,
549
struct u_rect *dst_rect,
550
enum vl_compositor_deinterlace deinterlace)
551
{
552
struct pipe_sampler_view **sampler_views;
553
unsigned i;
554
555
assert(s && c && buffer);
556
557
assert(layer < VL_COMPOSITOR_MAX_LAYERS);
558
559
s->interlaced = buffer->interlaced;
560
s->used_layers |= 1 << layer;
561
sampler_views = buffer->get_sampler_view_components(buffer);
562
for (i = 0; i < 3; ++i) {
563
s->layers[layer].samplers[i] = c->sampler_linear;
564
pipe_sampler_view_reference(&s->layers[layer].sampler_views[i], sampler_views[i]);
565
}
566
567
calc_src_and_dst(&s->layers[layer], buffer->width, buffer->height,
568
src_rect ? *src_rect : default_rect(&s->layers[layer]),
569
dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
570
571
if (buffer->interlaced) {
572
float half_a_line = 0.5f / s->layers[layer].zw.y;
573
switch(deinterlace) {
574
case VL_COMPOSITOR_NONE:
575
case VL_COMPOSITOR_MOTION_ADAPTIVE:
576
case VL_COMPOSITOR_WEAVE:
577
if (c->pipe_cs_composit_supported)
578
s->layers[layer].cs = c->cs_weave_rgb;
579
else if (c->pipe_gfx_supported)
580
s->layers[layer].fs = c->fs_weave_rgb;
581
break;
582
583
case VL_COMPOSITOR_BOB_TOP:
584
s->layers[layer].zw.x = 0.0f;
585
s->layers[layer].src.tl.y += half_a_line;
586
s->layers[layer].src.br.y += half_a_line;
587
if (c->pipe_cs_composit_supported)
588
s->layers[layer].cs = c->cs_video_buffer;
589
else if (c->pipe_gfx_supported)
590
s->layers[layer].fs = c->fs_video_buffer;
591
break;
592
593
case VL_COMPOSITOR_BOB_BOTTOM:
594
s->layers[layer].zw.x = 1.0f;
595
s->layers[layer].src.tl.y -= half_a_line;
596
s->layers[layer].src.br.y -= half_a_line;
597
if (c->pipe_cs_composit_supported)
598
s->layers[layer].cs = c->cs_video_buffer;
599
else if (c->pipe_gfx_supported)
600
s->layers[layer].fs = c->fs_video_buffer;
601
break;
602
}
603
604
} else {
605
if (c->pipe_cs_composit_supported)
606
s->layers[layer].cs = c->cs_video_buffer;
607
else if (c->pipe_gfx_supported)
608
s->layers[layer].fs = c->fs_video_buffer;
609
}
610
}
611
612
void
613
vl_compositor_set_palette_layer(struct vl_compositor_state *s,
614
struct vl_compositor *c,
615
unsigned layer,
616
struct pipe_sampler_view *indexes,
617
struct pipe_sampler_view *palette,
618
struct u_rect *src_rect,
619
struct u_rect *dst_rect,
620
bool include_color_conversion)
621
{
622
assert(s && c && indexes && palette);
623
624
assert(layer < VL_COMPOSITOR_MAX_LAYERS);
625
626
s->used_layers |= 1 << layer;
627
628
s->layers[layer].fs = include_color_conversion ?
629
c->fs_palette.yuv : c->fs_palette.rgb;
630
631
s->layers[layer].samplers[0] = c->sampler_linear;
632
s->layers[layer].samplers[1] = c->sampler_nearest;
633
s->layers[layer].samplers[2] = NULL;
634
pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], indexes);
635
pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], palette);
636
pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);
637
calc_src_and_dst(&s->layers[layer], indexes->texture->width0, indexes->texture->height0,
638
src_rect ? *src_rect : default_rect(&s->layers[layer]),
639
dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
640
}
641
642
void
643
vl_compositor_set_rgba_layer(struct vl_compositor_state *s,
644
struct vl_compositor *c,
645
unsigned layer,
646
struct pipe_sampler_view *rgba,
647
struct u_rect *src_rect,
648
struct u_rect *dst_rect,
649
struct vertex4f *colors)
650
{
651
unsigned i;
652
653
assert(s && c && rgba);
654
655
assert(layer < VL_COMPOSITOR_MAX_LAYERS);
656
657
s->used_layers |= 1 << layer;
658
s->layers[layer].fs = c->fs_rgba;
659
s->layers[layer].samplers[0] = c->sampler_linear;
660
s->layers[layer].samplers[1] = NULL;
661
s->layers[layer].samplers[2] = NULL;
662
pipe_sampler_view_reference(&s->layers[layer].sampler_views[0], rgba);
663
pipe_sampler_view_reference(&s->layers[layer].sampler_views[1], NULL);
664
pipe_sampler_view_reference(&s->layers[layer].sampler_views[2], NULL);
665
calc_src_and_dst(&s->layers[layer], rgba->texture->width0, rgba->texture->height0,
666
src_rect ? *src_rect : default_rect(&s->layers[layer]),
667
dst_rect ? *dst_rect : default_rect(&s->layers[layer]));
668
669
if (colors)
670
for (i = 0; i < 4; ++i)
671
s->layers[layer].colors[i] = colors[i];
672
}
673
674
void
675
vl_compositor_set_layer_rotation(struct vl_compositor_state *s,
676
unsigned layer,
677
enum vl_compositor_rotation rotate)
678
{
679
assert(s);
680
assert(layer < VL_COMPOSITOR_MAX_LAYERS);
681
s->layers[layer].rotate = rotate;
682
}
683
684
void
685
vl_compositor_yuv_deint_full(struct vl_compositor_state *s,
686
struct vl_compositor *c,
687
struct pipe_video_buffer *src,
688
struct pipe_video_buffer *dst,
689
struct u_rect *src_rect,
690
struct u_rect *dst_rect,
691
enum vl_compositor_deinterlace deinterlace)
692
{
693
struct pipe_surface **dst_surfaces;
694
695
dst_surfaces = dst->get_surfaces(dst);
696
vl_compositor_clear_layers(s);
697
698
set_yuv_layer(s, c, 0, src, src_rect, NULL, true, deinterlace);
699
vl_compositor_set_layer_dst_area(s, 0, dst_rect);
700
vl_compositor_render(s, c, dst_surfaces[0], NULL, false);
701
702
if (dst_rect) {
703
dst_rect->x1 /= 2;
704
dst_rect->y1 /= 2;
705
}
706
707
set_yuv_layer(s, c, 0, src, src_rect, NULL, false, deinterlace);
708
vl_compositor_set_layer_dst_area(s, 0, dst_rect);
709
vl_compositor_render(s, c, dst_surfaces[1], NULL, false);
710
711
s->pipe->flush(s->pipe, NULL, 0);
712
}
713
714
void
715
vl_compositor_convert_rgb_to_yuv(struct vl_compositor_state *s,
716
struct vl_compositor *c,
717
unsigned layer,
718
struct pipe_resource *src_res,
719
struct pipe_video_buffer *dst,
720
struct u_rect *src_rect,
721
struct u_rect *dst_rect)
722
{
723
struct pipe_sampler_view *sv, sv_templ;
724
struct pipe_surface **dst_surfaces;
725
726
dst_surfaces = dst->get_surfaces(dst);
727
728
memset(&sv_templ, 0, sizeof(sv_templ));
729
u_sampler_view_default_template(&sv_templ, src_res, src_res->format);
730
sv = s->pipe->create_sampler_view(s->pipe, src_res, &sv_templ);
731
732
vl_compositor_clear_layers(s);
733
734
set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, true);
735
vl_compositor_set_layer_dst_area(s, 0, dst_rect);
736
vl_compositor_render(s, c, dst_surfaces[0], NULL, false);
737
738
if (dst_rect) {
739
dst_rect->x1 /= 2;
740
dst_rect->y1 /= 2;
741
}
742
743
set_rgb_to_yuv_layer(s, c, 0, sv, src_rect, NULL, false);
744
vl_compositor_set_layer_dst_area(s, 0, dst_rect);
745
vl_compositor_render(s, c, dst_surfaces[1], NULL, false);
746
pipe_sampler_view_reference(&sv, NULL);
747
748
s->pipe->flush(s->pipe, NULL, 0);
749
}
750
751
void
752
vl_compositor_render(struct vl_compositor_state *s,
753
struct vl_compositor *c,
754
struct pipe_surface *dst_surface,
755
struct u_rect *dirty_area,
756
bool clear_dirty)
757
{
758
assert(s);
759
760
if (s->layers->cs)
761
vl_compositor_cs_render(s, c, dst_surface, dirty_area, clear_dirty);
762
else if (s->layers->fs)
763
vl_compositor_gfx_render(s, c, dst_surface, dirty_area, clear_dirty);
764
else
765
debug_warning("Hardware don't support.\n");;
766
}
767
768
bool
769
vl_compositor_init(struct vl_compositor *c, struct pipe_context *pipe)
770
{
771
assert(c);
772
773
memset(c, 0, sizeof(*c));
774
775
c->pipe_cs_composit_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_PREFER_COMPUTE_FOR_MULTIMEDIA) &&
776
pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_TEX_TXF_LZ) &&
777
pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_DIV);
778
779
c->pipe_gfx_supported = pipe->screen->get_param(pipe->screen, PIPE_CAP_GRAPHICS);
780
c->pipe = pipe;
781
782
c->deinterlace = VL_COMPOSITOR_NONE;
783
784
if (!init_pipe_state(c)) {
785
return false;
786
}
787
788
if (!init_shaders(c)) {
789
cleanup_pipe_state(c);
790
return false;
791
}
792
793
if (!init_buffers(c)) {
794
cleanup_shaders(c);
795
cleanup_pipe_state(c);
796
return false;
797
}
798
799
return true;
800
}
801
802
bool
803
vl_compositor_init_state(struct vl_compositor_state *s, struct pipe_context *pipe)
804
{
805
vl_csc_matrix csc_matrix;
806
807
assert(s);
808
809
memset(s, 0, sizeof(*s));
810
811
s->pipe = pipe;
812
813
s->clear_color.f[0] = s->clear_color.f[1] = 0.0f;
814
s->clear_color.f[2] = s->clear_color.f[3] = 0.0f;
815
816
/*
817
* Create our fragment shader's constant buffer
818
* Const buffer contains the color conversion matrix and bias vectors
819
*/
820
/* XXX: Create with IMMUTABLE/STATIC... although it does change every once in a long while... */
821
s->shader_params = pipe_buffer_create_const0
822
(
823
pipe->screen,
824
PIPE_BIND_CONSTANT_BUFFER,
825
PIPE_USAGE_DEFAULT,
826
sizeof(csc_matrix) + 6*sizeof(float) + 10*sizeof(int)
827
);
828
829
if (!s->shader_params)
830
return false;
831
832
vl_compositor_clear_layers(s);
833
834
vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, &csc_matrix);
835
if (!vl_compositor_set_csc_matrix(s, (const vl_csc_matrix *)&csc_matrix, 1.0f, 0.0f))
836
return false;
837
838
return true;
839
}
840
841
void
842
vl_compositor_cleanup_state(struct vl_compositor_state *s)
843
{
844
assert(s);
845
846
vl_compositor_clear_layers(s);
847
pipe_resource_reference(&s->shader_params, NULL);
848
}
849
850