1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
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"
41 sp_mpeg12_destroy(struct pipe_video_context
*vpipe
)
43 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
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
);
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
);
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
);
64 sp_mpeg12_get_param(struct pipe_video_context
*vpipe
, int param
)
66 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
71 case PIPE_CAP_NPOT_TEXTURES
:
72 /* XXX: Temporary; not all paths are NPOT-tested */
74 return ctx
->pipe
->screen
->get_param(ctx
->pipe
->screen
, param
);
77 case PIPE_CAP_DECODE_TARGET_PREFERRED_FORMAT
:
78 return ctx
->decode_format
;
81 debug_printf("Softpipe: Unknown PIPE_CAP %d\n", param
);
88 sp_mpeg12_is_format_supported(struct pipe_video_context
*vpipe
,
89 enum pipe_format format
,
93 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
97 /* XXX: Temporary; not all paths are NPOT-tested */
98 if (geom
& PIPE_TEXTURE_GEOM_NON_POWER_OF_TWO
)
101 return ctx
->pipe
->screen
->is_format_supported(ctx
->pipe
->screen
, format
, PIPE_TEXTURE_2D
,
106 sp_mpeg12_decode_macroblocks(struct pipe_video_context
*vpipe
,
107 struct pipe_surface
*past
,
108 struct pipe_surface
*future
,
109 unsigned num_macroblocks
,
110 struct pipe_macroblock
*macroblocks
,
111 struct pipe_fence_handle
**fence
)
113 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
114 struct pipe_mpeg12_macroblock
*mpeg12_macroblocks
= (struct pipe_mpeg12_macroblock
*)macroblocks
;
117 assert(num_macroblocks
);
119 assert(macroblocks
->codec
== PIPE_VIDEO_CODEC_MPEG12
);
120 assert(ctx
->decode_target
);
122 vl_mpeg12_mc_renderer_render_macroblocks(&ctx
->mc_renderer
,
124 past
, future
, num_macroblocks
,
125 mpeg12_macroblocks
, fence
);
129 sp_mpeg12_surface_fill(struct pipe_video_context
*vpipe
,
130 struct pipe_surface
*dst
,
131 unsigned dstx
, unsigned dsty
,
132 unsigned width
, unsigned height
,
135 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
136 float rgba
[4] = { 0, 0, 0, 0 };
141 if (ctx
->pipe
->clear_render_target
)
142 ctx
->pipe
->clear_render_target(ctx
->pipe
, dst
, rgba
, dstx
, dsty
, width
, height
);
144 util_clear_render_target(ctx
->pipe
, dst
, rgba
, dstx
, dsty
, width
, height
);
148 sp_mpeg12_surface_copy(struct pipe_video_context
*vpipe
,
149 struct pipe_surface
*dst
,
150 unsigned dstx
, unsigned dsty
,
151 struct pipe_surface
*src
,
152 unsigned srcx
, unsigned srcy
,
153 unsigned width
, unsigned height
)
155 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
160 struct pipe_subresource subdst
, subsrc
;
161 subdst
.face
= dst
->face
;
162 subdst
.level
= dst
->level
;
163 subsrc
.face
= src
->face
;
164 subsrc
.level
= src
->level
;
166 if (ctx
->pipe
->resource_copy_region
)
167 ctx
->pipe
->resource_copy_region(ctx
->pipe
, dst
->texture
, subdst
, dstx
, dsty
, dst
->zslice
,
168 src
->texture
, subsrc
, srcx
, srcy
, src
->zslice
,
171 util_resource_copy_region(ctx
->pipe
, dst
->texture
, subdst
, dstx
, dsty
, dst
->zslice
,
172 src
->texture
, subsrc
, srcx
, srcy
, src
->zslice
,
176 static struct pipe_transfer
*
177 sp_mpeg12_get_transfer(struct pipe_video_context
*vpipe
,
178 struct pipe_resource
*resource
,
179 struct pipe_subresource subresource
,
180 unsigned usage
, /* a combination of PIPE_TRANSFER_x */
181 const struct pipe_box
*box
)
183 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
189 return ctx
->pipe
->get_transfer(ctx
->pipe
, resource
, subresource
, usage
, box
);
193 sp_mpeg12_transfer_destroy(struct pipe_video_context
*vpipe
,
194 struct pipe_transfer
*transfer
)
196 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
201 ctx
->pipe
->transfer_destroy(ctx
->pipe
, transfer
);
205 sp_mpeg12_transfer_map(struct pipe_video_context
*vpipe
,
206 struct pipe_transfer
*transfer
)
208 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
213 return ctx
->pipe
->transfer_map(ctx
->pipe
, transfer
);
217 sp_mpeg12_transfer_flush_region(struct pipe_video_context
*vpipe
,
218 struct pipe_transfer
*transfer
,
219 const struct pipe_box
*box
)
221 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
227 ctx
->pipe
->transfer_flush_region(ctx
->pipe
, transfer
, box
);
231 sp_mpeg12_transfer_unmap(struct pipe_video_context
*vpipe
,
232 struct pipe_transfer
*transfer
)
234 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
239 ctx
->pipe
->transfer_unmap(ctx
->pipe
, transfer
);
243 sp_mpeg12_transfer_inline_write(struct pipe_video_context
*vpipe
,
244 struct pipe_resource
*resource
,
245 struct pipe_subresource subresource
,
246 unsigned usage
, /* a combination of PIPE_TRANSFER_x */
247 const struct pipe_box
*box
,
250 unsigned slice_stride
)
252 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
258 assert(ctx
->pipe
->transfer_inline_write
);
260 ctx
->pipe
->transfer_inline_write(ctx
->pipe
, resource
, subresource
, usage
,
261 box
, data
, stride
, slice_stride
);
265 sp_mpeg12_render_picture(struct pipe_video_context
*vpipe
,
266 struct pipe_surface
*src_surface
,
267 enum pipe_mpeg12_picture_type picture_type
,
268 /*unsigned num_past_surfaces,
269 struct pipe_surface *past_surfaces,
270 unsigned num_future_surfaces,
271 struct pipe_surface *future_surfaces,*/
272 struct pipe_video_rect
*src_area
,
273 struct pipe_surface
*dst_surface
,
274 struct pipe_video_rect
*dst_area
,
275 struct pipe_fence_handle
**fence
)
277 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
285 vl_compositor_render(&ctx
->compositor
, src_surface
,
286 picture_type
, src_area
, dst_surface
, dst_area
, fence
);
290 sp_mpeg12_set_picture_background(struct pipe_video_context
*vpipe
,
291 struct pipe_surface
*bg
,
292 struct pipe_video_rect
*bg_src_rect
)
294 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
300 vl_compositor_set_background(&ctx
->compositor
, bg
, bg_src_rect
);
304 sp_mpeg12_set_picture_layers(struct pipe_video_context
*vpipe
,
305 struct pipe_surface
*layers
[],
306 struct pipe_video_rect
*src_rects
[],
307 struct pipe_video_rect
*dst_rects
[],
310 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
313 assert((layers
&& src_rects
&& dst_rects
) ||
314 (!layers
&& !src_rects
&& !dst_rects
));
316 vl_compositor_set_layers(&ctx
->compositor
, layers
, src_rects
, dst_rects
, num_layers
);
320 sp_mpeg12_set_decode_target(struct pipe_video_context
*vpipe
,
321 struct pipe_surface
*dt
)
323 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
328 pipe_surface_reference(&ctx
->decode_target
, dt
);
332 sp_mpeg12_set_csc_matrix(struct pipe_video_context
*vpipe
, const float *mat
)
334 struct sp_mpeg12_context
*ctx
= (struct sp_mpeg12_context
*)vpipe
;
338 vl_compositor_set_csc_matrix(&ctx
->compositor
, mat
);
342 init_pipe_state(struct sp_mpeg12_context
*ctx
)
344 struct pipe_rasterizer_state rast
;
345 struct pipe_blend_state blend
;
346 struct pipe_depth_stencil_alpha_state dsa
;
351 memset(&rast
, 0, sizeof rast
);
353 rast
.flatshade_first
= 0;
354 rast
.light_twoside
= 0;
356 rast
.cull_face
= PIPE_FACE_NONE
;
357 rast
.fill_back
= PIPE_POLYGON_MODE_FILL
;
358 rast
.fill_front
= PIPE_POLYGON_MODE_FILL
;
359 rast
.offset_point
= 0;
360 rast
.offset_line
= 0;
362 rast
.poly_smooth
= 0;
363 rast
.poly_stipple_enable
= 0;
364 rast
.sprite_coord_enable
= 0;
365 rast
.point_size_per_vertex
= 0;
366 rast
.multisample
= 0;
367 rast
.line_smooth
= 0;
368 rast
.line_stipple_enable
= 0;
369 rast
.line_stipple_factor
= 0;
370 rast
.line_stipple_pattern
= 0;
371 rast
.line_last_pixel
= 0;
373 rast
.point_smooth
= 0;
374 rast
.point_quad_rasterization
= 0;
376 rast
.offset_units
= 1;
377 rast
.offset_scale
= 1;
378 rast
.gl_rasterization_rules
= 1;
379 ctx
->rast
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &rast
);
380 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rast
);
382 memset(&blend
, 0, sizeof blend
);
383 blend
.independent_blend_enable
= 0;
384 blend
.rt
[0].blend_enable
= 0;
385 blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
386 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
387 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ONE
;
388 blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
389 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
390 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ONE
;
391 blend
.logicop_enable
= 0;
392 blend
.logicop_func
= PIPE_LOGICOP_CLEAR
;
393 /* Needed to allow color writes to FB, even if blending disabled */
394 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
396 ctx
->blend
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &blend
);
397 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend
);
399 memset(&dsa
, 0, sizeof dsa
);
400 dsa
.depth
.enabled
= 0;
401 dsa
.depth
.writemask
= 0;
402 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
403 for (i
= 0; i
< 2; ++i
) {
404 dsa
.stencil
[i
].enabled
= 0;
405 dsa
.stencil
[i
].func
= PIPE_FUNC_ALWAYS
;
406 dsa
.stencil
[i
].fail_op
= PIPE_STENCIL_OP_KEEP
;
407 dsa
.stencil
[i
].zpass_op
= PIPE_STENCIL_OP_KEEP
;
408 dsa
.stencil
[i
].zfail_op
= PIPE_STENCIL_OP_KEEP
;
409 dsa
.stencil
[i
].valuemask
= 0;
410 dsa
.stencil
[i
].writemask
= 0;
412 dsa
.alpha
.enabled
= 0;
413 dsa
.alpha
.func
= PIPE_FUNC_ALWAYS
;
414 dsa
.alpha
.ref_value
= 0;
415 ctx
->dsa
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &dsa
);
416 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->dsa
);
421 static struct pipe_video_context
*
422 sp_mpeg12_create(struct pipe_context
*pipe
, enum pipe_video_profile profile
,
423 enum pipe_video_chroma_format chroma_format
,
424 unsigned width
, unsigned height
,
425 enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode
,
426 enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling
,
428 enum pipe_format decode_format
)
430 struct sp_mpeg12_context
*ctx
;
432 assert(u_reduce_video_profile(profile
) == PIPE_VIDEO_CODEC_MPEG12
);
434 ctx
= CALLOC_STRUCT(sp_mpeg12_context
);
439 ctx
->base
.profile
= profile
;
440 ctx
->base
.chroma_format
= chroma_format
;
441 ctx
->base
.width
= width
;
442 ctx
->base
.height
= height
;
444 ctx
->base
.screen
= pipe
->screen
;
445 ctx
->base
.destroy
= sp_mpeg12_destroy
;
446 ctx
->base
.get_param
= sp_mpeg12_get_param
;
447 ctx
->base
.is_format_supported
= sp_mpeg12_is_format_supported
;
448 ctx
->base
.decode_macroblocks
= sp_mpeg12_decode_macroblocks
;
449 ctx
->base
.render_picture
= sp_mpeg12_render_picture
;
450 ctx
->base
.surface_fill
= sp_mpeg12_surface_fill
;
451 ctx
->base
.surface_copy
= sp_mpeg12_surface_copy
;
452 ctx
->base
.get_transfer
= sp_mpeg12_get_transfer
;
453 ctx
->base
.transfer_destroy
= sp_mpeg12_transfer_destroy
;
454 ctx
->base
.transfer_map
= sp_mpeg12_transfer_map
;
455 ctx
->base
.transfer_flush_region
= sp_mpeg12_transfer_flush_region
;
456 ctx
->base
.transfer_unmap
= sp_mpeg12_transfer_unmap
;
457 if (pipe
->transfer_inline_write
)
458 ctx
->base
.transfer_inline_write
= sp_mpeg12_transfer_inline_write
;
459 ctx
->base
.set_picture_background
= sp_mpeg12_set_picture_background
;
460 ctx
->base
.set_picture_layers
= sp_mpeg12_set_picture_layers
;
461 ctx
->base
.set_decode_target
= sp_mpeg12_set_decode_target
;
462 ctx
->base
.set_csc_matrix
= sp_mpeg12_set_csc_matrix
;
465 ctx
->decode_format
= decode_format
;
467 if (!vl_mpeg12_mc_renderer_init(&ctx
->mc_renderer
, ctx
->pipe
,
468 width
, height
, chroma_format
,
469 bufmode
, eb_handling
, pot_buffers
)) {
470 ctx
->pipe
->destroy(ctx
->pipe
);
475 if (!vl_compositor_init(&ctx
->compositor
, ctx
->pipe
)) {
476 vl_mpeg12_mc_renderer_cleanup(&ctx
->mc_renderer
);
477 ctx
->pipe
->destroy(ctx
->pipe
);
482 if (!init_pipe_state(ctx
)) {
483 vl_compositor_cleanup(&ctx
->compositor
);
484 vl_mpeg12_mc_renderer_cleanup(&ctx
->mc_renderer
);
485 ctx
->pipe
->destroy(ctx
->pipe
);
493 struct pipe_video_context
*
494 sp_video_create(struct pipe_screen
*screen
, enum pipe_video_profile profile
,
495 enum pipe_video_chroma_format chroma_format
,
496 unsigned width
, unsigned height
, void *priv
)
498 struct pipe_context
*pipe
;
501 assert(width
&& height
);
503 pipe
= screen
->context_create(screen
, NULL
);
507 /* TODO: Use slice buffering for softpipe when implemented, no advantage to buffering an entire picture with softpipe */
508 /* TODO: Use XFER_NONE when implemented */
509 return sp_video_create_ex(pipe
, profile
,
512 VL_MPEG12_MC_RENDERER_BUFFER_PICTURE
,
513 VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE
,
518 struct pipe_video_context
*
519 sp_video_create_ex(struct pipe_context
*pipe
, enum pipe_video_profile profile
,
520 enum pipe_video_chroma_format chroma_format
,
521 unsigned width
, unsigned height
,
522 enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode
,
523 enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling
,
525 enum pipe_format decode_format
)
528 assert(width
&& height
);
530 switch (u_reduce_video_profile(profile
)) {
531 case PIPE_VIDEO_CODEC_MPEG12
:
532 return sp_mpeg12_create(pipe
, profile
,
535 bufmode
, eb_handling
,