vl: WIP DRI2 support in the winsys.
[mesa.git] / src / gallium / auxiliary / vl / vl_compositor.c
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 "vl_compositor.h"
29 #include <assert.h>
30 #include <pipe/p_context.h>
31 #include <util/u_inlines.h>
32 #include <util/u_memory.h>
33 #include <tgsi/tgsi_ureg.h>
34 #include "vl_csc.h"
35
36 struct vertex_shader_consts
37 {
38 struct vertex4f dst_scale;
39 struct vertex4f dst_trans;
40 struct vertex4f src_scale;
41 struct vertex4f src_trans;
42 };
43
44 struct fragment_shader_consts
45 {
46 float matrix[16];
47 };
48
49 static bool
50 u_video_rects_equal(struct pipe_video_rect *a, struct pipe_video_rect *b)
51 {
52 assert(a && b);
53
54 if (a->x != b->x)
55 return false;
56 if (a->y != b->y)
57 return false;
58 if (a->w != b->w)
59 return false;
60 if (a->h != b->h)
61 return false;
62
63 return true;
64 }
65
66 static bool
67 create_vert_shader(struct vl_compositor *c)
68 {
69 struct ureg_program *shader;
70 struct ureg_src vpos, vtex;
71 struct ureg_dst o_vpos, o_vtex;
72
73 shader = ureg_create(TGSI_PROCESSOR_VERTEX);
74 if (!shader)
75 return false;
76
77 vpos = ureg_DECL_vs_input(shader, 0);
78 vtex = ureg_DECL_vs_input(shader, 1);
79 o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, 0);
80 o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, 1);
81
82 /*
83 * o_vpos = vpos
84 * o_vtex = vtex
85 */
86 ureg_MOV(shader, o_vpos, vpos);
87 ureg_MOV(shader, o_vtex, vtex);
88
89 ureg_END(shader);
90
91 c->vertex_shader = ureg_create_shader_and_destroy(shader, c->pipe);
92 if (!c->vertex_shader)
93 return false;
94
95 return true;
96 }
97
98 static bool
99 create_frag_shader(struct vl_compositor *c)
100 {
101 struct ureg_program *shader;
102 struct ureg_src tc;
103 struct ureg_src csc[4];
104 struct ureg_src sampler;
105 struct ureg_dst texel;
106 struct ureg_dst fragment;
107 unsigned i;
108
109 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
110 if (!shader)
111 return false;
112
113 tc = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, 1, TGSI_INTERPOLATE_LINEAR);
114 for (i = 0; i < 4; ++i)
115 csc[i] = ureg_DECL_constant(shader, i);
116 sampler = ureg_DECL_sampler(shader, 0);
117 texel = ureg_DECL_temporary(shader);
118 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
119
120 /*
121 * texel = tex(tc, sampler)
122 * fragment = csc * texel
123 */
124 ureg_TEX(shader, texel, TGSI_TEXTURE_2D, tc, sampler);
125 for (i = 0; i < 4; ++i)
126 ureg_DP4(shader, ureg_writemask(fragment, TGSI_WRITEMASK_X << i), csc[i], ureg_src(texel));
127
128 ureg_release_temporary(shader, texel);
129 ureg_END(shader);
130
131 c->fragment_shader = ureg_create_shader_and_destroy(shader, c->pipe);
132 if (!c->fragment_shader)
133 return false;
134
135 return true;
136 }
137
138 static bool
139 init_pipe_state(struct vl_compositor *c)
140 {
141 struct pipe_sampler_state sampler;
142
143 assert(c);
144
145 c->fb_state.nr_cbufs = 1;
146 c->fb_state.zsbuf = NULL;
147
148 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
149 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
150 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
151 sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
152 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
153 sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
154 sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
155 sampler.compare_func = PIPE_FUNC_ALWAYS;
156 sampler.normalized_coords = 1;
157 /*sampler.lod_bias = ;*/
158 /*sampler.min_lod = ;*/
159 /*sampler.max_lod = ;*/
160 /*sampler.border_color[i] = ;*/
161 /*sampler.max_anisotropy = ;*/
162 c->sampler = c->pipe->create_sampler_state(c->pipe, &sampler);
163
164 return true;
165 }
166
167 static void cleanup_pipe_state(struct vl_compositor *c)
168 {
169 assert(c);
170
171 c->pipe->delete_sampler_state(c->pipe, c->sampler);
172 }
173
174 static bool
175 init_shaders(struct vl_compositor *c)
176 {
177 assert(c);
178
179 create_vert_shader(c);
180 create_frag_shader(c);
181
182 return true;
183 }
184
185 static void cleanup_shaders(struct vl_compositor *c)
186 {
187 assert(c);
188
189 c->pipe->delete_vs_state(c->pipe, c->vertex_shader);
190 c->pipe->delete_fs_state(c->pipe, c->fragment_shader);
191 }
192
193 static bool
194 init_buffers(struct vl_compositor *c)
195 {
196 struct fragment_shader_consts fsc;
197
198 assert(c);
199
200 /*
201 * Create our vertex buffer and vertex buffer elements
202 */
203 c->vertex_buf.stride = sizeof(struct vertex4f);
204 c->vertex_buf.max_index = (VL_COMPOSITOR_MAX_LAYERS + 2) * 6 - 1;
205 c->vertex_buf.buffer_offset = 0;
206 c->vertex_buf.buffer = pipe_buffer_create
207 (
208 c->pipe->screen,
209 1,
210 PIPE_BUFFER_USAGE_VERTEX,
211 sizeof(struct vertex4f) * (VL_COMPOSITOR_MAX_LAYERS + 2) * 6
212 );
213
214 c->vertex_elems[0].src_offset = 0;
215 c->vertex_elems[0].instance_divisor = 0;
216 c->vertex_elems[0].vertex_buffer_index = 0;
217 c->vertex_elems[0].nr_components = 2;
218 c->vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT;
219 c->vertex_elems[1].src_offset = sizeof(struct vertex2f);
220 c->vertex_elems[1].instance_divisor = 0;
221 c->vertex_elems[1].vertex_buffer_index = 0;
222 c->vertex_elems[1].nr_components = 2;
223 c->vertex_elems[1].src_format = PIPE_FORMAT_R32G32_FLOAT;
224
225 /*
226 * Create our fragment shader's constant buffer
227 * Const buffer contains the color conversion matrix and bias vectors
228 */
229 c->fs_const_buf = pipe_buffer_create
230 (
231 c->pipe->screen,
232 1,
233 PIPE_BUFFER_USAGE_CONSTANT,
234 sizeof(struct fragment_shader_consts)
235 );
236
237 vl_csc_get_matrix(VL_CSC_COLOR_STANDARD_IDENTITY, NULL, true, fsc.matrix);
238
239 vl_compositor_set_csc_matrix(c, fsc.matrix);
240
241 return true;
242 }
243
244 static void
245 cleanup_buffers(struct vl_compositor *c)
246 {
247 assert(c);
248
249 pipe_buffer_reference(&c->vertex_buf.buffer, NULL);
250 pipe_buffer_reference(&c->fs_const_buf, NULL);
251 }
252
253 bool vl_compositor_init(struct vl_compositor *compositor, struct pipe_context *pipe)
254 {
255 unsigned i;
256
257 assert(compositor);
258
259 memset(compositor, 0, sizeof(struct vl_compositor));
260
261 compositor->pipe = pipe;
262
263 if (!init_pipe_state(compositor))
264 return false;
265 if (!init_shaders(compositor)) {
266 cleanup_pipe_state(compositor);
267 return false;
268 }
269 if (!init_buffers(compositor)) {
270 cleanup_shaders(compositor);
271 cleanup_pipe_state(compositor);
272 return false;
273 }
274
275 compositor->fb_state.width = 0;
276 compositor->fb_state.height = 0;
277 compositor->bg = NULL;
278 compositor->dirty_bg = false;
279 for (i = 0; i < VL_COMPOSITOR_MAX_LAYERS; ++i)
280 compositor->layers[i] = NULL;
281 compositor->dirty_layers = 0;
282
283 return true;
284 }
285
286 void vl_compositor_cleanup(struct vl_compositor *compositor)
287 {
288 assert(compositor);
289
290 cleanup_buffers(compositor);
291 cleanup_shaders(compositor);
292 cleanup_pipe_state(compositor);
293 }
294
295 void vl_compositor_set_background(struct vl_compositor *compositor,
296 struct pipe_surface *bg, struct pipe_video_rect *bg_src_rect)
297 {
298 assert(compositor);
299 assert((bg && bg_src_rect) || (!bg && !bg_src_rect));
300
301 if (compositor->bg != bg ||
302 !u_video_rects_equal(&compositor->bg_src_rect, bg_src_rect)) {
303 pipe_surface_reference(&compositor->bg, bg);
304 /*if (!u_video_rects_equal(&compositor->bg_src_rect, bg_src_rect))*/
305 compositor->bg_src_rect = *bg_src_rect;
306 compositor->dirty_bg = true;
307 }
308 }
309
310 void vl_compositor_set_layers(struct vl_compositor *compositor,
311 struct pipe_surface *layers[],
312 struct pipe_video_rect *src_rects[],
313 struct pipe_video_rect *dst_rects[],
314 unsigned num_layers)
315 {
316 unsigned i;
317
318 assert(compositor);
319 assert(num_layers <= VL_COMPOSITOR_MAX_LAYERS);
320
321 for (i = 0; i < num_layers; ++i)
322 {
323 assert((layers[i] && src_rects[i] && dst_rects[i]) ||
324 (!layers[i] && !src_rects[i] && !dst_rects[i]));
325
326 if (compositor->layers[i] != layers[i] ||
327 !u_video_rects_equal(&compositor->layer_src_rects[i], src_rects[i]) ||
328 !u_video_rects_equal(&compositor->layer_dst_rects[i], dst_rects[i]))
329 {
330 pipe_surface_reference(&compositor->layers[i], layers[i]);
331 /*if (!u_video_rects_equal(&compositor->layer_src_rects[i], src_rects[i]))*/
332 compositor->layer_src_rects[i] = *src_rects[i];
333 /*if (!u_video_rects_equal(&compositor->layer_dst_rects[i], dst_rects[i]))*/
334 compositor->layer_dst_rects[i] = *dst_rects[i];
335 compositor->dirty_layers |= 1 << i;
336 }
337 }
338
339 for (; i < VL_COMPOSITOR_MAX_LAYERS; ++i)
340 pipe_surface_reference(&compositor->layers[i], NULL);
341 }
342
343 static void gen_rect_verts(unsigned pos,
344 struct pipe_video_rect *src_rect,
345 struct vertex2f *src_inv_size,
346 struct pipe_video_rect *dst_rect,
347 struct vertex2f *dst_inv_size,
348 struct vertex4f *vb)
349 {
350 assert(pos < VL_COMPOSITOR_MAX_LAYERS + 2);
351 assert(src_rect);
352 assert(src_inv_size);
353 assert((dst_rect && dst_inv_size) /*|| (!dst_rect && !dst_inv_size)*/);
354 assert(vb);
355
356 vb[pos * 6 + 0].x = dst_rect->x * dst_inv_size->x;
357 vb[pos * 6 + 0].y = dst_rect->y * dst_inv_size->y;
358 vb[pos * 6 + 0].z = src_rect->x * src_inv_size->x;
359 vb[pos * 6 + 0].w = src_rect->y * src_inv_size->y;
360
361 vb[pos * 6 + 1].x = dst_rect->x * dst_inv_size->x;
362 vb[pos * 6 + 1].y = (dst_rect->y + dst_rect->h) * dst_inv_size->y;
363 vb[pos * 6 + 1].z = src_rect->x * src_inv_size->x;
364 vb[pos * 6 + 1].w = (src_rect->y + src_rect->h) * src_inv_size->y;
365
366 vb[pos * 6 + 2].x = (dst_rect->x + dst_rect->w) * dst_inv_size->x;
367 vb[pos * 6 + 2].y = dst_rect->y * dst_inv_size->y;
368 vb[pos * 6 + 2].z = (src_rect->x + src_rect->w) * src_inv_size->x;
369 vb[pos * 6 + 2].w = src_rect->y * src_inv_size->y;
370
371 vb[pos * 6 + 3].x = (dst_rect->x + dst_rect->w) * dst_inv_size->x;
372 vb[pos * 6 + 3].y = dst_rect->y * dst_inv_size->y;
373 vb[pos * 6 + 3].z = (src_rect->x + src_rect->w) * src_inv_size->x;
374 vb[pos * 6 + 3].w = src_rect->y * src_inv_size->y;
375
376 vb[pos * 6 + 4].x = dst_rect->x * dst_inv_size->x;
377 vb[pos * 6 + 4].y = (dst_rect->y + dst_rect->h) * dst_inv_size->y;
378 vb[pos * 6 + 4].z = src_rect->x * src_inv_size->x;
379 vb[pos * 6 + 4].w = (src_rect->y + src_rect->h) * src_inv_size->y;
380
381 vb[pos * 6 + 5].x = (dst_rect->x + dst_rect->w) * dst_inv_size->x;
382 vb[pos * 6 + 5].y = (dst_rect->y + dst_rect->h) * dst_inv_size->y;
383 vb[pos * 6 + 5].z = (src_rect->x + src_rect->w) * src_inv_size->x;
384 vb[pos * 6 + 5].w = (src_rect->y + src_rect->h) * src_inv_size->y;
385 }
386
387 static unsigned gen_data(struct vl_compositor *c,
388 struct pipe_surface *src_surface,
389 struct pipe_video_rect *src_rect,
390 struct pipe_video_rect *dst_rect,
391 struct pipe_surface **textures)
392 {
393 void *vb;
394 unsigned num_rects = 0;
395 unsigned i;
396
397 assert(c);
398 assert(src_surface);
399 assert(src_rect);
400 assert(dst_rect);
401 assert(textures);
402
403 vb = pipe_buffer_map(c->pipe->screen, c->vertex_buf.buffer,
404 PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD);
405
406 if (!vb)
407 return 0;
408
409 if (c->dirty_bg) {
410 struct vertex2f bg_inv_size = {1.0f / c->bg->width, 1.0f / c->bg->height};
411 gen_rect_verts(num_rects, &c->bg_src_rect, &bg_inv_size, NULL, NULL, vb);
412 textures[num_rects] = c->bg;
413 ++num_rects;
414 c->dirty_bg = false;
415 }
416
417 {
418 struct vertex2f src_inv_size = { 1.0f / src_surface->width, 1.0f / src_surface->height};
419 gen_rect_verts(num_rects, src_rect, &src_inv_size, dst_rect, &c->fb_inv_size, vb);
420 textures[num_rects] = src_surface;
421 ++num_rects;
422 }
423
424 for (i = 0; c->dirty_layers > 0; i++) {
425 assert(i < VL_COMPOSITOR_MAX_LAYERS);
426
427 if (c->dirty_layers & (1 << i)) {
428 struct vertex2f layer_inv_size = {1.0f / c->layers[i]->width, 1.0f / c->layers[i]->height};
429 gen_rect_verts(num_rects, &c->layer_src_rects[i], &layer_inv_size,
430 &c->layer_dst_rects[i], &c->fb_inv_size, vb);
431 textures[num_rects] = c->layers[i];
432 ++num_rects;
433 c->dirty_layers &= ~(1 << i);
434 }
435 }
436
437 pipe_buffer_unmap(c->pipe->screen, c->vertex_buf.buffer);
438
439 return num_rects;
440 }
441
442 static void draw_layers(struct vl_compositor *c,
443 struct pipe_surface *src_surface,
444 struct pipe_video_rect *src_rect,
445 struct pipe_video_rect *dst_rect)
446 {
447 unsigned num_rects;
448 struct pipe_surface *src_surfaces[VL_COMPOSITOR_MAX_LAYERS + 2];
449 unsigned i;
450
451 assert(c);
452 assert(src_surface);
453 assert(src_rect);
454 assert(dst_rect);
455
456 num_rects = gen_data(c, src_surface, src_rect, dst_rect, src_surfaces);
457
458 for (i = 0; i < num_rects; ++i) {
459 c->pipe->set_fragment_sampler_textures(c->pipe, 1, &src_surfaces[i]->texture);
460 c->pipe->draw_arrays(c->pipe, PIPE_PRIM_TRIANGLES, i * 6, 6);
461 }
462 }
463
464 void vl_compositor_render(struct vl_compositor *compositor,
465 struct pipe_surface *src_surface,
466 enum pipe_mpeg12_picture_type picture_type,
467 /*unsigned num_past_surfaces,
468 struct pipe_surface *past_surfaces,
469 unsigned num_future_surfaces,
470 struct pipe_surface *future_surfaces,*/
471 struct pipe_video_rect *src_area,
472 struct pipe_surface *dst_surface,
473 struct pipe_video_rect *dst_area,
474 struct pipe_fence_handle **fence)
475 {
476 assert(compositor);
477 assert(src_surface);
478 assert(src_area);
479 assert(dst_surface);
480 assert(dst_area);
481 assert(picture_type == PIPE_MPEG12_PICTURE_TYPE_FRAME);
482
483 if (compositor->fb_state.width != dst_surface->width) {
484 compositor->fb_inv_size.x = 1.0f / dst_surface->width;
485 compositor->fb_state.width = dst_surface->width;
486 }
487 if (compositor->fb_state.height != dst_surface->height) {
488 compositor->fb_inv_size.y = 1.0f / dst_surface->height;
489 compositor->fb_state.height = dst_surface->height;
490 }
491
492 compositor->fb_state.cbufs[0] = dst_surface;
493
494 compositor->viewport.scale[0] = compositor->fb_state.width;
495 compositor->viewport.scale[1] = compositor->fb_state.height;
496 compositor->viewport.scale[2] = 1;
497 compositor->viewport.scale[3] = 1;
498 compositor->viewport.translate[0] = 0;
499 compositor->viewport.translate[1] = 0;
500 compositor->viewport.translate[2] = 0;
501 compositor->viewport.translate[3] = 0;
502
503 compositor->pipe->set_framebuffer_state(compositor->pipe, &compositor->fb_state);
504 compositor->pipe->set_viewport_state(compositor->pipe, &compositor->viewport);
505 compositor->pipe->bind_fragment_sampler_states(compositor->pipe, 1, &compositor->sampler);
506 compositor->pipe->bind_vs_state(compositor->pipe, compositor->vertex_shader);
507 compositor->pipe->bind_fs_state(compositor->pipe, compositor->fragment_shader);
508 compositor->pipe->set_vertex_buffers(compositor->pipe, 1, &compositor->vertex_buf);
509 compositor->pipe->set_vertex_elements(compositor->pipe, 2, compositor->vertex_elems);
510 compositor->pipe->set_constant_buffer(compositor->pipe, PIPE_SHADER_FRAGMENT, 0, compositor->fs_const_buf);
511
512 draw_layers(compositor, src_surface, src_area, dst_area);
513
514 assert(!compositor->dirty_bg && !compositor->dirty_layers);
515 compositor->pipe->flush(compositor->pipe, PIPE_FLUSH_RENDER_CACHE, fence);
516 }
517
518 void vl_compositor_set_csc_matrix(struct vl_compositor *compositor, const float *mat)
519 {
520 assert(compositor);
521
522 memcpy
523 (
524 pipe_buffer_map(compositor->pipe->screen, compositor->fs_const_buf,
525 PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD),
526 mat,
527 sizeof(struct fragment_shader_consts)
528 );
529
530 pipe_buffer_unmap(compositor->pipe->screen, compositor->fs_const_buf);
531 }