vl_compositor.c revision 76d881b8b086495081c0a3c8fea2278f1480f107
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 TUNGSTEN GRAPHICS 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 <assert.h>
29
30#include <pipe/p_context.h>
31
32#include <util/u_memory.h>
33#include <util/u_draw.h>
34
35#include <tgsi/tgsi_ureg.h>
36
37#include "vl_csc.h"
38#include "vl_types.h"
39#include "vl_compositor.h"
40
41typedef float csc_matrix[16];
42
43static void *
44create_vert_shader(struct vl_compositor *c)
45{
46   struct ureg_program *shader;
47   struct ureg_src vpos, vtex;
48   struct ureg_dst o_vpos, o_vtex;
49
50   shader = ureg_create(TGSI_PROCESSOR_VERTEX);
51   if (!shader)
52      return false;
53
54   vpos = ureg_DECL_vs_input(shader, 0);
55   vtex = ureg_DECL_vs_input(shader, 1);
56   o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, 0);
57   o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, 1);
58
59   /*
60    * o_vpos = vpos
61    * o_vtex = vtex
62    */
63   ureg_MOV(shader, o_vpos, vpos);
64   ureg_MOV(shader, o_vtex, vtex);
65
66   ureg_END(shader);
67
68   return ureg_create_shader_and_destroy(shader, c->pipe);
69}
70
71static void *
72create_frag_shader_video_buffer(struct vl_compositor *c)
73{
74   struct ureg_program *shader;
75   struct ureg_src tc;
76   struct ureg_src csc[3];
77   struct ureg_src sampler[3];
78   struct ureg_dst texel;
79   struct ureg_dst fragment;
80   unsigned i;
81
82   shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
83   if (!shader)
84      return false;
85
86   tc = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, 1, TGSI_INTERPOLATE_LINEAR);
87   for (i = 0; i < 3; ++i) {
88      csc[i] = ureg_DECL_constant(shader, i);
89      sampler[i] = ureg_DECL_sampler(shader, i);
90   }
91   texel = ureg_DECL_temporary(shader);
92   fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
93
94   /*
95    * texel.xyz = tex(tc, sampler[i])
96    * fragment = csc * texel
97    */
98   for (i = 0; i < 3; ++i)
99      ureg_TEX(shader, ureg_writemask(texel, TGSI_WRITEMASK_X << i), TGSI_TEXTURE_2D, tc, sampler[i]);
100
101   ureg_MOV(shader, ureg_writemask(texel, TGSI_WRITEMASK_W), ureg_imm1f(shader, 1.0f));
102
103   for (i = 0; i < 3; ++i)
104      ureg_DP4(shader, ureg_writemask(fragment, TGSI_WRITEMASK_X << i), csc[i], ureg_src(texel));
105
106   ureg_MOV(shader, ureg_writemask(fragment, TGSI_WRITEMASK_W), ureg_imm1f(shader, 1.0f));
107
108   ureg_release_temporary(shader, texel);
109   ureg_END(shader);
110
111   return ureg_create_shader_and_destroy(shader, c->pipe);
112}
113
114static void *
115create_frag_shader_palette(struct vl_compositor *c)
116{
117   struct ureg_program *shader;
118   struct ureg_src csc[3];
119   struct ureg_src tc;
120   struct ureg_src sampler;
121   struct ureg_src palette;
122   struct ureg_dst texel;
123   struct ureg_dst fragment;
124   unsigned i;
125
126   shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
127   if (!shader)
128      return false;
129
130   for (i = 0; i < 3; ++i)
131      csc[i] = ureg_DECL_constant(shader, i);
132
133   tc = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, 1, TGSI_INTERPOLATE_LINEAR);
134   sampler = ureg_DECL_sampler(shader, 0);
135   palette = ureg_DECL_sampler(shader, 1);
136   texel = ureg_DECL_temporary(shader);
137   fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
138
139   /*
140    * texel = tex(tc, sampler)
141    * fragment.xyz = tex(texel, palette) * csc
142    * fragment.a = texel.a
143    */
144   ureg_TEX(shader, texel, TGSI_TEXTURE_2D, tc, sampler);
145   ureg_MUL(shader, ureg_writemask(texel, TGSI_WRITEMASK_X), ureg_src(texel), ureg_imm1f(shader, 15.0f / 16.0f));
146   ureg_MOV(shader, ureg_writemask(fragment, TGSI_WRITEMASK_W), ureg_src(texel));
147
148   ureg_TEX(shader, texel, TGSI_TEXTURE_1D, ureg_src(texel), palette);
149
150   for (i = 0; i < 3; ++i)
151      ureg_DP4(shader, ureg_writemask(fragment, TGSI_WRITEMASK_X << i), csc[i], ureg_src(texel));
152
153   ureg_release_temporary(shader, texel);
154   ureg_END(shader);
155
156   return ureg_create_shader_and_destroy(shader, c->pipe);
157}
158
159static void *
160create_frag_shader_rgba(struct vl_compositor *c)
161{
162   struct ureg_program *shader;
163   struct ureg_src tc;
164   struct ureg_src sampler;
165   struct ureg_dst fragment;
166
167   shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
168   if (!shader)
169      return false;
170
171   tc = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, 1, TGSI_INTERPOLATE_LINEAR);
172   sampler = ureg_DECL_sampler(shader, 0);
173   fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
174
175   /*
176    * fragment = tex(tc, sampler)
177    */
178   ureg_TEX(shader, fragment, TGSI_TEXTURE_2D, tc, sampler);
179   ureg_END(shader);
180
181   return ureg_create_shader_and_destroy(shader, c->pipe);
182}
183
184static bool
185init_shaders(struct vl_compositor *c)
186{
187   assert(c);
188
189   c->vs = create_vert_shader(c);
190   if (!c->vs) {
191      debug_printf("Unable to create vertex shader.\n");
192      return false;
193   }
194
195   c->fs_video_buffer = create_frag_shader_video_buffer(c);
196   if (!c->fs_video_buffer) {
197      debug_printf("Unable to create YCbCr-to-RGB fragment shader.\n");
198      return false;
199   }
200
201   c->fs_palette = create_frag_shader_palette(c);
202   if (!c->fs_palette) {
203      debug_printf("Unable to create Palette-to-RGB fragment shader.\n");
204      return false;
205   }
206
207   c->fs_rgba = create_frag_shader_rgba(c);
208   if (!c->fs_rgba) {
209      debug_printf("Unable to create RGB-to-RGB fragment shader.\n");
210      return false;
211   }
212
213   return true;
214}
215
216static void cleanup_shaders(struct vl_compositor *c)
217{
218   assert(c);
219
220   c->pipe->delete_vs_state(c->pipe, c->vs);
221   c->pipe->delete_fs_state(c->pipe, c->fs_video_buffer);
222   c->pipe->delete_fs_state(c->pipe, c->fs_palette);
223   c->pipe->delete_fs_state(c->pipe, c->fs_rgba);
224}
225
226static bool
227init_pipe_state(struct vl_compositor *c)
228{
229   struct pipe_rasterizer_state rast;
230   struct pipe_sampler_state sampler;
231   struct pipe_blend_state blend;
232
233   assert(c);
234
235   c->fb_state.nr_cbufs = 1;
236   c->fb_state.zsbuf = NULL;
237
238   c->viewport.scale[2] = 1;
239   c->viewport.scale[3] = 1;
240   c->viewport.translate[0] = 0;
241   c->viewport.translate[1] = 0;
242   c->viewport.translate[2] = 0;
243   c->viewport.translate[3] = 0;
244
245   memset(&sampler, 0, sizeof(sampler));
246   sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
247   sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
248   sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
249   sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
250   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
251   sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
252   sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
253   sampler.compare_func = PIPE_FUNC_ALWAYS;
254   sampler.normalized_coords = 1;
255
256   c->sampler_linear = c->pipe->create_sampler_state(c->pipe, &sampler);
257
258   sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
259   sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
260   c->sampler_nearest = c->pipe->create_sampler_state(c->pipe, &sampler);
261
262   memset(&blend, 0, sizeof blend);
263   blend.independent_blend_enable = 0;
264   blend.rt[0].blend_enable = 1;
265   blend.rt[0].rgb_func = PIPE_BLEND_ADD;
266   blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
267   blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
268   blend.rt[0].alpha_func = PIPE_BLEND_ADD;
269   blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
270   blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
271   blend.logicop_enable = 0;
272   blend.logicop_func = PIPE_LOGICOP_CLEAR;
273   blend.rt[0].colormask = PIPE_MASK_RGBA;
274   blend.dither = 0;
275   c->blend = c->pipe->create_blend_state(c->pipe, &blend);
276
277   memset(&rast, 0, sizeof rast);
278   rast.flatshade = 1;
279   rast.front_ccw = 1;
280   rast.cull_face = PIPE_FACE_NONE;
281   rast.fill_back = PIPE_POLYGON_MODE_FILL;
282   rast.fill_front = PIPE_POLYGON_MODE_FILL;
283   rast.scissor = 1;
284   rast.line_width = 1;
285   rast.point_size_per_vertex = 1;
286   rast.offset_units = 1;
287   rast.offset_scale = 1;
288   rast.gl_rasterization_rules = 1;
289
290   c->rast = c->pipe->create_rasterizer_state(c->pipe, &rast);
291
292   return true;
293}
294
295static void cleanup_pipe_state(struct vl_compositor *c)
296{
297   assert(c);
298
299   c->pipe->delete_sampler_state(c->pipe, c->sampler_linear);
300   c->pipe->delete_sampler_state(c->pipe, c->sampler_nearest);
301   c->pipe->delete_blend_state(c->pipe, c->blend);
302   c->pipe->delete_rasterizer_state(c->pipe, c->rast);
303}
304
305static bool
306init_buffers(struct vl_compositor *c)
307{
308   struct pipe_vertex_element vertex_elems[2];
309
310   assert(c);
311
312   /*
313    * Create our vertex buffer and vertex buffer elements
314    */
315   c->vertex_buf.stride = sizeof(struct vertex4f);
316   c->vertex_buf.buffer_offset = 0;
317   c->vertex_buf.buffer = pipe_buffer_create
318   (
319      c->pipe->screen,
320      PIPE_BIND_VERTEX_BUFFER,
321      PIPE_USAGE_STREAM,
322      sizeof(struct vertex4f) * (VL_COMPOSITOR_MAX_LAYERS + 1) * 4
323   );
324
325   vertex_elems[0].src_offset = 0;
326   vertex_elems[0].instance_divisor = 0;
327   vertex_elems[0].vertex_buffer_index = 0;
328   vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT;
329   vertex_elems[1].src_offset = sizeof(struct vertex2f);
330   vertex_elems[1].instance_divisor = 0;
331   vertex_elems[1].vertex_buffer_index = 0;
332   vertex_elems[1].src_format = PIPE_FORMAT_R32G32_FLOAT;
333   c->vertex_elems_state = c->pipe->create_vertex_elements_state(c->pipe, 2, vertex_elems);
334
335   /*
336    * Create our fragment shader's constant buffer
337    * Const buffer contains the color conversion matrix and bias vectors
338    */
339   /* XXX: Create with IMMUTABLE/STATIC... although it does change every once in a long while... */
340   c->csc_matrix = pipe_buffer_create
341   (
342      c->pipe->screen,
343      PIPE_BIND_CONSTANT_BUFFER,
344      PIPE_USAGE_STATIC,
345      sizeof(csc_matrix)
346   );
347
348   return true;
349}
350
351static void
352cleanup_buffers(struct vl_compositor *c)
353{
354   assert(c);
355
356   c->pipe->delete_vertex_elements_state(c->pipe, c->vertex_elems_state);
357   pipe_resource_reference(&c->vertex_buf.buffer, NULL);
358   pipe_resource_reference(&c->csc_matrix, NULL);
359}
360
361static inline struct pipe_video_rect
362default_rect(struct vl_compositor_layer *layer)
363{
364   struct pipe_resource *res = layer->sampler_views[0]->texture;
365   struct pipe_video_rect rect = { 0, 0, res->width0, res->height0 };
366   return rect;
367}
368
369static void
370gen_rect_verts(struct vertex4f *vb,
371               struct pipe_video_rect *src_rect,
372               struct vertex2f *src_inv_size,
373               struct pipe_video_rect *dst_rect,
374               struct vertex2f *dst_inv_size)
375{
376   assert(vb);
377   assert(src_rect && src_inv_size);
378   assert(dst_rect && dst_inv_size);
379
380   vb[0].x = dst_rect->x * dst_inv_size->x;
381   vb[0].y = dst_rect->y * dst_inv_size->y;
382   vb[0].z = src_rect->x * src_inv_size->x;
383   vb[0].w = src_rect->y * src_inv_size->y;
384
385   vb[1].x = (dst_rect->x + dst_rect->w) * dst_inv_size->x;
386   vb[1].y = dst_rect->y * dst_inv_size->y;
387   vb[1].z = (src_rect->x + src_rect->w) * src_inv_size->x;
388   vb[1].w = src_rect->y * src_inv_size->y;
389
390   vb[2].x = (dst_rect->x + dst_rect->w) * dst_inv_size->x;
391   vb[2].y = (dst_rect->y + dst_rect->h) * dst_inv_size->y;
392   vb[2].z = (src_rect->x + src_rect->w) * src_inv_size->x;
393   vb[2].w = (src_rect->y + src_rect->h) * src_inv_size->y;
394
395   vb[3].x = dst_rect->x * dst_inv_size->x;
396   vb[3].y = (dst_rect->y + dst_rect->h) * dst_inv_size->y;
397   vb[3].z = src_rect->x * src_inv_size->x;
398   vb[3].w = (src_rect->y + src_rect->h) * src_inv_size->y;
399}
400
401static void
402gen_vertex_data(struct vl_compositor *c)
403{
404   struct vertex4f *vb;
405   struct pipe_transfer *buf_transfer;
406   unsigned i;
407
408   assert(c);
409
410   vb = pipe_buffer_map(c->pipe, c->vertex_buf.buffer,
411                        PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD | PIPE_TRANSFER_DONTBLOCK,
412                        &buf_transfer);
413
414   if (!vb)
415      return;
416
417   for (i = 0; i < VL_COMPOSITOR_MAX_LAYERS; i++) {
418      if (c->used_layers & (1 << i)) {
419         struct pipe_sampler_view *sv = c->layers[i].sampler_views[0];
420         struct vertex2f src_inv_size = {1.0f / sv->texture->width0, 1.0f / sv->texture->height0};
421
422         gen_rect_verts(vb, &c->layers[i].src_rect, &src_inv_size, &c->layers[i].dst_rect, &src_inv_size);
423
424         vb += 4;
425      }
426   }
427
428   pipe_buffer_unmap(c->pipe, buf_transfer);
429}
430
431static void
432draw_layers(struct vl_compositor *c)
433{
434   unsigned vb_index, i;
435
436   assert(c);
437
438   for (i = 0, vb_index = 0; i < VL_COMPOSITOR_MAX_LAYERS; ++i) {
439      if (c->used_layers & (1 << i)) {
440         struct pipe_sampler_view **samplers = &c->layers[i].sampler_views[0];
441         unsigned num_sampler_views = !samplers[1] ? 1 : !samplers[2] ? 2 : 3;
442
443         c->pipe->bind_fs_state(c->pipe, c->layers[i].fs);
444         c->pipe->bind_fragment_sampler_states(c->pipe, num_sampler_views, c->layers[i].samplers);
445         c->pipe->set_fragment_sampler_views(c->pipe, num_sampler_views, samplers);
446         util_draw_arrays(c->pipe, PIPE_PRIM_QUADS, vb_index * 4, 4);
447         vb_index++;
448      }
449   }
450}
451
452static void
453vl_compositor_clear_layers(struct pipe_video_compositor *compositor)
454{
455   struct vl_compositor *c = (struct vl_compositor *)compositor;
456   unsigned i, j;
457
458   assert(compositor);
459
460   c->used_layers = 0;
461   for ( i = 0; i < VL_COMPOSITOR_MAX_LAYERS; ++i) {
462      c->layers[i].fs = NULL;
463      for ( j = 0; j < 3; j++)
464         pipe_sampler_view_reference(&c->layers[i].sampler_views[j], NULL);
465   }
466}
467
468static void
469vl_compositor_destroy(struct pipe_video_compositor *compositor)
470{
471   struct vl_compositor *c = (struct vl_compositor *)compositor;
472   assert(compositor);
473
474   vl_compositor_clear_layers(compositor);
475
476   cleanup_buffers(c);
477   cleanup_shaders(c);
478   cleanup_pipe_state(c);
479
480   FREE(compositor);
481}
482
483static void
484vl_compositor_set_csc_matrix(struct pipe_video_compositor *compositor, const float matrix[16])
485{
486   struct vl_compositor *c = (struct vl_compositor *)compositor;
487   struct pipe_transfer *buf_transfer;
488
489   assert(compositor);
490
491   memcpy
492   (
493      pipe_buffer_map(c->pipe, c->csc_matrix,
494                      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
495                      &buf_transfer),
496		matrix,
497		sizeof(csc_matrix)
498   );
499
500   pipe_buffer_unmap(c->pipe, buf_transfer);
501}
502
503static void
504vl_compositor_set_buffer_layer(struct pipe_video_compositor *compositor,
505                               unsigned layer,
506                               struct pipe_video_buffer *buffer,
507                               struct pipe_video_rect *src_rect,
508                               struct pipe_video_rect *dst_rect)
509{
510   struct vl_compositor *c = (struct vl_compositor *)compositor;
511   struct pipe_sampler_view **sampler_views;
512   unsigned i;
513
514   assert(compositor && buffer);
515
516   assert(layer < VL_COMPOSITOR_MAX_LAYERS);
517
518   c->used_layers |= 1 << layer;
519   c->layers[layer].fs = c->fs_video_buffer;
520
521   sampler_views = buffer->get_sampler_view_components(buffer);
522   for (i = 0; i < 3; ++i) {
523      c->layers[layer].samplers[i] = c->sampler_linear;
524      pipe_sampler_view_reference(&c->layers[layer].sampler_views[i], sampler_views[i]);
525   }
526
527   c->layers[layer].src_rect = src_rect ? *src_rect : default_rect(&c->layers[layer]);
528   c->layers[layer].dst_rect = dst_rect ? *dst_rect : default_rect(&c->layers[layer]);
529}
530
531static void
532vl_compositor_set_palette_layer(struct pipe_video_compositor *compositor,
533                                unsigned layer,
534                                struct pipe_sampler_view *indexes,
535                                struct pipe_sampler_view *palette,
536                                struct pipe_video_rect *src_rect,
537                                struct pipe_video_rect *dst_rect)
538{
539   struct vl_compositor *c = (struct vl_compositor *)compositor;
540   assert(compositor && indexes && palette);
541
542   assert(layer < VL_COMPOSITOR_MAX_LAYERS);
543
544   c->used_layers |= 1 << layer;
545   c->layers[layer].fs = c->fs_palette;
546   c->layers[layer].samplers[0] = c->sampler_linear;
547   c->layers[layer].samplers[1] = c->sampler_nearest;
548   c->layers[layer].samplers[2] = NULL;
549   pipe_sampler_view_reference(&c->layers[layer].sampler_views[0], indexes);
550   pipe_sampler_view_reference(&c->layers[layer].sampler_views[1], palette);
551   pipe_sampler_view_reference(&c->layers[layer].sampler_views[2], NULL);
552   c->layers[layer].src_rect = src_rect ? *src_rect : default_rect(&c->layers[layer]);
553   c->layers[layer].dst_rect = dst_rect ? *dst_rect : default_rect(&c->layers[layer]);
554}
555
556static void
557vl_compositor_set_rgba_layer(struct pipe_video_compositor *compositor,
558                             unsigned layer,
559                             struct pipe_sampler_view *rgba,
560                             struct pipe_video_rect *src_rect,
561                             struct pipe_video_rect *dst_rect)
562{
563   struct vl_compositor *c = (struct vl_compositor *)compositor;
564   assert(compositor && rgba);
565
566   assert(layer < VL_COMPOSITOR_MAX_LAYERS);
567
568   c->used_layers |= 1 << layer;
569   c->layers[layer].fs = c->fs_rgba;
570   c->layers[layer].samplers[0] = c->sampler_linear;
571   c->layers[layer].samplers[1] = NULL;
572   c->layers[layer].samplers[2] = NULL;
573   pipe_sampler_view_reference(&c->layers[layer].sampler_views[0], rgba);
574   pipe_sampler_view_reference(&c->layers[layer].sampler_views[1], NULL);
575   pipe_sampler_view_reference(&c->layers[layer].sampler_views[2], NULL);
576   c->layers[layer].src_rect = src_rect ? *src_rect : default_rect(&c->layers[layer]);
577   c->layers[layer].dst_rect = dst_rect ? *dst_rect : default_rect(&c->layers[layer]);
578}
579
580static void
581vl_compositor_render(struct pipe_video_compositor *compositor,
582                     enum pipe_mpeg12_picture_type picture_type,
583                     struct pipe_surface           *dst_surface,
584                     struct pipe_video_rect        *dst_area,
585                     struct pipe_fence_handle      **fence)
586{
587   struct vl_compositor *c = (struct vl_compositor *)compositor;
588   struct pipe_scissor_state scissor;
589
590   assert(compositor);
591   assert(dst_surface);
592
593   c->fb_state.width = dst_surface->width;
594   c->fb_state.height = dst_surface->height;
595   c->fb_state.cbufs[0] = dst_surface;
596
597   c->viewport.scale[0] = dst_surface->width;
598   c->viewport.scale[1] = dst_surface->height;
599
600   if (dst_area) {
601      scissor.minx = dst_area->x;
602      scissor.miny = dst_area->y;
603      scissor.maxx = dst_area->x + dst_area->w;
604      scissor.maxy = dst_area->y + dst_area->h;
605   } else {
606      scissor.minx = 0;
607      scissor.miny = 0;
608      scissor.maxx = dst_surface->width;
609      scissor.maxy = dst_surface->height;
610   }
611
612   gen_vertex_data(c);
613
614   c->pipe->set_scissor_state(c->pipe, &scissor);
615   c->pipe->set_framebuffer_state(c->pipe, &c->fb_state);
616   c->pipe->set_viewport_state(c->pipe, &c->viewport);
617   c->pipe->bind_vs_state(c->pipe, c->vs);
618   c->pipe->set_vertex_buffers(c->pipe, 1, &c->vertex_buf);
619   c->pipe->bind_vertex_elements_state(c->pipe, c->vertex_elems_state);
620   c->pipe->set_constant_buffer(c->pipe, PIPE_SHADER_FRAGMENT, 0, c->csc_matrix);
621   c->pipe->bind_blend_state(c->pipe, c->blend);
622   c->pipe->bind_rasterizer_state(c->pipe, c->rast);
623
624   draw_layers(c);
625
626   c->pipe->flush(c->pipe, fence);
627}
628
629struct pipe_video_compositor *
630vl_compositor_init(struct pipe_video_context *vpipe, struct pipe_context *pipe)
631{
632   csc_matrix csc_matrix;
633   struct vl_compositor *compositor;
634
635   compositor = CALLOC_STRUCT(vl_compositor);
636
637   compositor->base.context = vpipe;
638   compositor->base.destroy = vl_compositor_destroy;
639   compositor->base.set_csc_matrix = vl_compositor_set_csc_matrix;
640   compositor->base.clear_layers = vl_compositor_clear_layers;
641   compositor->base.set_buffer_layer = vl_compositor_set_buffer_layer;
642   compositor->base.set_palette_layer = vl_compositor_set_palette_layer;
643   compositor->base.set_rgba_layer = vl_compositor_set_rgba_layer;
644   compositor->base.render_picture = vl_compositor_render;
645
646   compositor->pipe = pipe;
647
648   if (!init_pipe_state(compositor))
649      return false;
650
651   if (!init_shaders(compositor)) {
652      cleanup_pipe_state(compositor);
653      return false;
654   }
655   if (!init_buffers(compositor)) {
656      cleanup_shaders(compositor);
657      cleanup_pipe_state(compositor);
658      return false;
659   }
660
661   vl_compositor_clear_layers(&compositor->base);
662
663   vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, csc_matrix);
664   vl_compositor_set_csc_matrix(&compositor->base, csc_matrix);
665
666   return &compositor->base;
667}
668