Merge branch 'pipe-video' of git://anongit.freedesktop.org/~deathsimple/xvmc-r600...
[mesa.git] / src / gallium / drivers / softpipe / sp_video_context.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 "util/u_inlines.h"
29 #include "util/u_memory.h"
30
31 #include "sp_video_context.h"
32 #include <util/u_inlines.h>
33 #include <util/u_memory.h>
34 #include <util/u_rect.h>
35 #include <util/u_video.h>
36 #include <util/u_surface.h>
37 #include "sp_public.h"
38 #include "sp_texture.h"
39
40 static void
41 sp_mpeg12_destroy(struct pipe_video_context *vpipe)
42 {
43 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
44
45 assert(vpipe);
46
47 /* Asserted in softpipe_delete_fs_state() for some reason */
48 ctx->pipe->bind_vs_state(ctx->pipe, NULL);
49 ctx->pipe->bind_fs_state(ctx->pipe, NULL);
50
51 ctx->pipe->delete_blend_state(ctx->pipe, ctx->blend);
52 ctx->pipe->delete_rasterizer_state(ctx->pipe, ctx->rast);
53 ctx->pipe->delete_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
54
55 pipe_surface_reference(&ctx->decode_target, NULL);
56 vl_compositor_cleanup(&ctx->compositor);
57 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
58 ctx->pipe->destroy(ctx->pipe);
59
60 FREE(ctx);
61 }
62
63 static int
64 sp_mpeg12_get_param(struct pipe_video_context *vpipe, int param)
65 {
66 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
67
68 assert(vpipe);
69
70 switch (param) {
71 case PIPE_CAP_NPOT_TEXTURES:
72 /* XXX: Temporary; not all paths are NPOT-tested */
73 #if 0
74 return ctx->pipe->screen->get_param(ctx->pipe->screen, param);
75 #endif
76 return FALSE;
77 case PIPE_CAP_DECODE_TARGET_PREFERRED_FORMAT:
78 return ctx->decode_format;
79 default:
80 {
81 debug_printf("Softpipe: Unknown PIPE_CAP %d\n", param);
82 return 0;
83 }
84 }
85 }
86
87 static boolean
88 sp_mpeg12_is_format_supported(struct pipe_video_context *vpipe,
89 enum pipe_format format,
90 unsigned usage,
91 unsigned geom)
92 {
93 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
94
95 assert(vpipe);
96
97 /* XXX: Temporary; not all paths are NPOT-tested */
98 if (geom & PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO)
99 return FALSE;
100
101 <<<<<<< HEAD
102 return ctx->pipe->screen->is_format_supported(ctx->pipe->screen, format, PIPE_TEXTURE_2D, 1,
103 usage, geom);
104 =======
105 return ctx->pipe->screen->is_format_supported(ctx->pipe->screen, format, PIPE_TEXTURE_2D,
106 0, usage, geom);
107 >>>>>>> 97a7cf230a70c64fff300931ae7c00aa00449c97
108 }
109
110 static void
111 sp_mpeg12_decode_macroblocks(struct pipe_video_context *vpipe,
112 struct pipe_surface *past,
113 struct pipe_surface *future,
114 unsigned num_macroblocks,
115 struct pipe_macroblock *macroblocks,
116 struct pipe_fence_handle **fence)
117 {
118 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
119 struct pipe_mpeg12_macroblock *mpeg12_macroblocks = (struct pipe_mpeg12_macroblock*)macroblocks;
120
121 assert(vpipe);
122 assert(num_macroblocks);
123 assert(macroblocks);
124 assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
125 assert(ctx->decode_target);
126
127 vl_mpeg12_mc_renderer_render_macroblocks(&ctx->mc_renderer,
128 ctx->decode_target,
129 past, future, num_macroblocks,
130 mpeg12_macroblocks, fence);
131 }
132
133 static void
134 sp_mpeg12_clear_render_target(struct pipe_video_context *vpipe,
135 struct pipe_surface *dst,
136 unsigned dstx, unsigned dsty,
137 const float *rgba,
138 unsigned width, unsigned height)
139 {
140 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
141 float rgba[4] = { 0, 0, 0, 0 };
142
143 assert(vpipe);
144 assert(dst);
145
146 if (ctx->pipe->clear_render_target)
147 ctx->pipe->clear_render_target(ctx->pipe, dst, rgba, dstx, dsty, width, height);
148 else
149 util_clear_render_target(ctx->pipe, dst, rgba, dstx, dsty, width, height);
150 }
151
152 static void
153 sp_mpeg12_resource_copy_region(struct pipe_video_context *vpipe,
154 struct pipe_resource *dst,
155 struct pipe_subresource subdst,
156 unsigned dstx, unsigned dsty, unsigned dstz,
157 struct pipe_resource *src,
158 struct pipe_subresource subsrc,
159 unsigned srcx, unsigned srcy, unsigned srcz,
160 unsigned width, unsigned height)
161 {
162 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
163
164 assert(vpipe);
165 assert(dst);
166
167 <<<<<<< HEAD
168 if (ctx->pipe->resource_copy_region)
169 ctx->pipe->resource_copy_region(ctx->pipe, dst, subdst, dstx, dsty, dstz, src, subsrc, srcx, srcy, srcz, width, height);
170 else
171 util_resource_copy_region(ctx->pipe, dst, subdst, dstx, dsty, dstz, src, subsrc, srcx, srcy, srcz, width, height);
172 =======
173 struct pipe_subresource subdst, subsrc;
174 subdst.face = dst->face;
175 subdst.level = dst->level;
176 subsrc.face = src->face;
177 subsrc.level = src->level;
178
179 if (ctx->pipe->resource_copy_region)
180 ctx->pipe->resource_copy_region(ctx->pipe, dst->texture, subdst, dstx, dsty, dst->zslice,
181 src->texture, subsrc, srcx, srcy, src->zslice,
182 width, height);
183 else
184 util_resource_copy_region(ctx->pipe, dst->texture, subdst, dstx, dsty, dst->zslice,
185 src->texture, subsrc, srcx, srcy, src->zslice,
186 width, height);
187 >>>>>>> 97a7cf230a70c64fff300931ae7c00aa00449c97
188 }
189
190 static struct pipe_transfer*
191 sp_mpeg12_get_transfer(struct pipe_video_context *vpipe,
192 struct pipe_resource *resource,
193 struct pipe_subresource subresource,
194 unsigned usage, /* a combination of PIPE_TRANSFER_x */
195 const struct pipe_box *box)
196 {
197 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
198
199 assert(vpipe);
200 assert(resource);
201 assert(box);
202
203 return ctx->pipe->get_transfer(ctx->pipe, resource, subresource, usage, box);
204 }
205
206 static void
207 sp_mpeg12_transfer_destroy(struct pipe_video_context *vpipe,
208 struct pipe_transfer *transfer)
209 {
210 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
211
212 assert(vpipe);
213 assert(transfer);
214
215 ctx->pipe->transfer_destroy(ctx->pipe, transfer);
216 }
217
218 static void*
219 sp_mpeg12_transfer_map(struct pipe_video_context *vpipe,
220 struct pipe_transfer *transfer)
221 {
222 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
223
224 assert(vpipe);
225 assert(transfer);
226
227 return ctx->pipe->transfer_map(ctx->pipe, transfer);
228 }
229
230 static void
231 sp_mpeg12_transfer_flush_region(struct pipe_video_context *vpipe,
232 struct pipe_transfer *transfer,
233 const struct pipe_box *box)
234 {
235 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
236
237 assert(vpipe);
238 assert(transfer);
239 assert(box);
240
241 ctx->pipe->transfer_flush_region(ctx->pipe, transfer, box);
242 }
243
244 static void
245 sp_mpeg12_transfer_unmap(struct pipe_video_context *vpipe,
246 struct pipe_transfer *transfer)
247 {
248 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
249
250 assert(vpipe);
251 assert(transfer);
252
253 ctx->pipe->transfer_unmap(ctx->pipe, transfer);
254 }
255
256 static void
257 sp_mpeg12_transfer_inline_write(struct pipe_video_context *vpipe,
258 struct pipe_resource *resource,
259 struct pipe_subresource subresource,
260 unsigned usage, /* a combination of PIPE_TRANSFER_x */
261 const struct pipe_box *box,
262 const void *data,
263 unsigned stride,
264 unsigned slice_stride)
265 {
266 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
267
268 assert(vpipe);
269 assert(resource);
270 assert(box);
271 assert(data);
272 assert(ctx->pipe->transfer_inline_write);
273
274 ctx->pipe->transfer_inline_write(ctx->pipe, resource, subresource, usage,
275 box, data, stride, slice_stride);
276 }
277
278 static void
279 sp_mpeg12_render_picture(struct pipe_video_context *vpipe,
280 struct pipe_surface *src_surface,
281 enum pipe_mpeg12_picture_type picture_type,
282 /*unsigned num_past_surfaces,
283 struct pipe_surface *past_surfaces,
284 unsigned num_future_surfaces,
285 struct pipe_surface *future_surfaces,*/
286 struct pipe_video_rect *src_area,
287 struct pipe_surface *dst_surface,
288 struct pipe_video_rect *dst_area,
289 struct pipe_fence_handle **fence)
290 {
291 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
292
293 assert(vpipe);
294 assert(src_surface);
295 assert(src_area);
296 assert(dst_surface);
297 assert(dst_area);
298
299 vl_compositor_render(&ctx->compositor, src_surface,
300 picture_type, src_area, dst_surface, dst_area, fence);
301 }
302
303 static void
304 sp_mpeg12_set_picture_background(struct pipe_video_context *vpipe,
305 struct pipe_surface *bg,
306 struct pipe_video_rect *bg_src_rect)
307 {
308 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
309
310 assert(vpipe);
311 assert(bg);
312 assert(bg_src_rect);
313
314 vl_compositor_set_background(&ctx->compositor, bg, bg_src_rect);
315 }
316
317 static void
318 sp_mpeg12_set_picture_layers(struct pipe_video_context *vpipe,
319 struct pipe_surface *layers[],
320 struct pipe_video_rect *src_rects[],
321 struct pipe_video_rect *dst_rects[],
322 unsigned num_layers)
323 {
324 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
325
326 assert(vpipe);
327 assert((layers && src_rects && dst_rects) ||
328 (!layers && !src_rects && !dst_rects));
329
330 vl_compositor_set_layers(&ctx->compositor, layers, src_rects, dst_rects, num_layers);
331 }
332
333 static void
334 sp_mpeg12_set_decode_target(struct pipe_video_context *vpipe,
335 struct pipe_surface *dt)
336 {
337 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
338
339 assert(vpipe);
340 assert(dt);
341
342 pipe_surface_reference(&ctx->decode_target, dt);
343 }
344
345 static void
346 sp_mpeg12_set_csc_matrix(struct pipe_video_context *vpipe, const float *mat)
347 {
348 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
349
350 assert(vpipe);
351
352 vl_compositor_set_csc_matrix(&ctx->compositor, mat);
353 }
354
355 static bool
356 init_pipe_state(struct sp_mpeg12_context *ctx)
357 {
358 struct pipe_rasterizer_state rast;
359 struct pipe_blend_state blend;
360 struct pipe_depth_stencil_alpha_state dsa;
361 unsigned i;
362
363 assert(ctx);
364
365 memset(&rast, 0, sizeof rast);
366 rast.flatshade = 1;
367 rast.flatshade_first = 0;
368 rast.light_twoside = 0;
369 <<<<<<< HEAD
370 rast.cull_face = PIPE_FACE_FRONT;
371 rast.fill_front = PIPE_POLYGON_MODE_FILL;
372 rast.fill_back = PIPE_POLYGON_MODE_FILL;
373 =======
374 rast.front_ccw = 1;
375 rast.cull_face = PIPE_FACE_NONE;
376 rast.fill_back = PIPE_POLYGON_MODE_FILL;
377 rast.fill_front = PIPE_POLYGON_MODE_FILL;
378 rast.offset_point = 0;
379 rast.offset_line = 0;
380 >>>>>>> 97a7cf230a70c64fff300931ae7c00aa00449c97
381 rast.scissor = 0;
382 rast.poly_smooth = 0;
383 rast.poly_stipple_enable = 0;
384 rast.sprite_coord_enable = 0;
385 rast.point_size_per_vertex = 0;
386 rast.multisample = 0;
387 rast.line_smooth = 0;
388 rast.line_stipple_enable = 0;
389 rast.line_stipple_factor = 0;
390 rast.line_stipple_pattern = 0;
391 rast.line_last_pixel = 0;
392 rast.line_width = 1;
393 rast.point_smooth = 0;
394 rast.point_quad_rasterization = 0;
395 rast.point_size_per_vertex = 1;
396 rast.offset_units = 1;
397 rast.offset_scale = 1;
398 rast.gl_rasterization_rules = 1;
399
400 ctx->rast = ctx->pipe->create_rasterizer_state(ctx->pipe, &rast);
401 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rast);
402
403 <<<<<<< HEAD
404
405 =======
406 memset(&blend, 0, sizeof blend);
407 >>>>>>> 97a7cf230a70c64fff300931ae7c00aa00449c97
408 blend.independent_blend_enable = 0;
409 blend.rt[0].blend_enable = 0;
410 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
411 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
412 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
413 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
414 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
415 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
416 blend.logicop_enable = 0;
417 blend.logicop_func = PIPE_LOGICOP_CLEAR;
418 /* Needed to allow color writes to FB, even if blending disabled */
419 blend.rt[0].colormask = PIPE_MASK_RGBA;
420 blend.dither = 0;
421 ctx->blend = ctx->pipe->create_blend_state(ctx->pipe, &blend);
422 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend);
423
424 memset(&dsa, 0, sizeof dsa);
425 dsa.depth.enabled = 0;
426 dsa.depth.writemask = 0;
427 dsa.depth.func = PIPE_FUNC_ALWAYS;
428 for (i = 0; i < 2; ++i) {
429 dsa.stencil[i].enabled = 0;
430 dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
431 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
432 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
433 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
434 dsa.stencil[i].valuemask = 0;
435 dsa.stencil[i].writemask = 0;
436 }
437 dsa.alpha.enabled = 0;
438 dsa.alpha.func = PIPE_FUNC_ALWAYS;
439 dsa.alpha.ref_value = 0;
440 ctx->dsa = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &dsa);
441 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
442
443 return true;
444 }
445
446 static struct pipe_video_context *
447 sp_mpeg12_create(struct pipe_context *pipe, enum pipe_video_profile profile,
448 enum pipe_video_chroma_format chroma_format,
449 unsigned width, unsigned height,
450 enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode,
451 enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling,
452 bool pot_buffers,
453 enum pipe_format decode_format)
454 {
455 struct sp_mpeg12_context *ctx;
456
457 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
458
459 ctx = CALLOC_STRUCT(sp_mpeg12_context);
460
461 if (!ctx)
462 return NULL;
463
464 ctx->base.profile = profile;
465 ctx->base.chroma_format = chroma_format;
466 ctx->base.width = width;
467 ctx->base.height = height;
468
469 ctx->base.screen = pipe->screen;
470
471 ctx->base.destroy = sp_mpeg12_destroy;
472 ctx->base.get_param = sp_mpeg12_get_param;
473 ctx->base.is_format_supported = sp_mpeg12_is_format_supported;
474 ctx->base.decode_macroblocks = sp_mpeg12_decode_macroblocks;
475 ctx->base.render_picture = sp_mpeg12_render_picture;
476 ctx->base.clear_render_target = sp_mpeg12_clear_render_target;
477 ctx->base.resource_copy_region = sp_mpeg12_resource_copy_region;
478 ctx->base.get_transfer = sp_mpeg12_get_transfer;
479 ctx->base.transfer_destroy = sp_mpeg12_transfer_destroy;
480 ctx->base.transfer_map = sp_mpeg12_transfer_map;
481 ctx->base.transfer_flush_region = sp_mpeg12_transfer_flush_region;
482 ctx->base.transfer_unmap = sp_mpeg12_transfer_unmap;
483 if (pipe->transfer_inline_write)
484 ctx->base.transfer_inline_write = sp_mpeg12_transfer_inline_write;
485 ctx->base.set_picture_background = sp_mpeg12_set_picture_background;
486 ctx->base.set_picture_layers = sp_mpeg12_set_picture_layers;
487 ctx->base.set_decode_target = sp_mpeg12_set_decode_target;
488 ctx->base.set_csc_matrix = sp_mpeg12_set_csc_matrix;
489
490 ctx->pipe = pipe;
491 ctx->decode_format = decode_format;
492
493 if (!vl_mpeg12_mc_renderer_init(&ctx->mc_renderer, ctx->pipe,
494 width, height, chroma_format,
495 bufmode, eb_handling, pot_buffers)) {
496 ctx->pipe->destroy(ctx->pipe);
497 FREE(ctx);
498 return NULL;
499 }
500
501 if (!vl_compositor_init(&ctx->compositor, ctx->pipe)) {
502 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
503 ctx->pipe->destroy(ctx->pipe);
504 FREE(ctx);
505 return NULL;
506 }
507
508 if (!init_pipe_state(ctx)) {
509 vl_compositor_cleanup(&ctx->compositor);
510 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
511 ctx->pipe->destroy(ctx->pipe);
512 FREE(ctx);
513 return NULL;
514 }
515
516 return &ctx->base;
517 }
518
519 struct pipe_video_context *
520 sp_video_create(struct pipe_screen *screen, enum pipe_video_profile profile,
521 enum pipe_video_chroma_format chroma_format,
522 unsigned width, unsigned height, void *priv)
523 {
524 struct pipe_context *pipe;
525
526 assert(screen);
527 assert(width && height);
528
529 pipe = screen->context_create(screen, NULL);
530 if (!pipe)
531 return NULL;
532
533 /* TODO: Use slice buffering for softpipe when implemented, no advantage to buffering an entire picture with softpipe */
534 /* TODO: Use XFER_NONE when implemented */
535 return sp_video_create_ex(pipe, profile,
536 chroma_format,
537 width, height,
538 VL_MPEG12_MC_RENDERER_BUFFER_PICTURE,
539 VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE,
540 true,
541 PIPE_FORMAT_XYUV);
542 }
543
544 struct pipe_video_context *
545 sp_video_create_ex(struct pipe_context *pipe, enum pipe_video_profile profile,
546 enum pipe_video_chroma_format chroma_format,
547 unsigned width, unsigned height,
548 enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode,
549 enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling,
550 bool pot_buffers,
551 enum pipe_format decode_format)
552 {
553 assert(pipe);
554 assert(width && height);
555
556 switch (u_reduce_video_profile(profile)) {
557 case PIPE_VIDEO_CODEC_MPEG12:
558 return sp_mpeg12_create(pipe, profile,
559 chroma_format,
560 width, height,
561 bufmode, eb_handling,
562 pot_buffers,
563 decode_format);
564 default:
565 return NULL;
566 }
567 }