Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into pipe-video
[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 return ctx->pipe->screen->is_format_supported(ctx->pipe->screen, format, PIPE_TEXTURE_2D,
102 0, usage, geom);
103 }
104
105 static void
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)
112 {
113 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
114 struct pipe_mpeg12_macroblock *mpeg12_macroblocks = (struct pipe_mpeg12_macroblock*)macroblocks;
115
116 assert(vpipe);
117 assert(num_macroblocks);
118 assert(macroblocks);
119 assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
120 assert(ctx->decode_target);
121
122 vl_mpeg12_mc_renderer_render_macroblocks(&ctx->mc_renderer,
123 ctx->decode_target,
124 past, future, num_macroblocks,
125 mpeg12_macroblocks, fence);
126 }
127
128 static void
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,
133 unsigned value)
134 {
135 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
136 float rgba[4] = { 0, 0, 0, 0 };
137
138 assert(vpipe);
139 assert(dst);
140
141 if (ctx->pipe->clear_render_target)
142 ctx->pipe->clear_render_target(ctx->pipe, dst, rgba, dstx, dsty, width, height);
143 else
144 util_clear_render_target(ctx->pipe, dst, rgba, dstx, dsty, width, height);
145 }
146
147 static void
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)
154 {
155 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
156
157 assert(vpipe);
158 assert(dst);
159
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;
165
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,
169 width, height);
170 else
171 util_resource_copy_region(ctx->pipe, dst->texture, subdst, dstx, dsty, dst->zslice,
172 src->texture, subsrc, srcx, srcy, src->zslice,
173 width, height);
174 }
175
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)
182 {
183 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
184
185 assert(vpipe);
186 assert(resource);
187 assert(box);
188
189 return ctx->pipe->get_transfer(ctx->pipe, resource, subresource, usage, box);
190 }
191
192 static void
193 sp_mpeg12_transfer_destroy(struct pipe_video_context *vpipe,
194 struct pipe_transfer *transfer)
195 {
196 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
197
198 assert(vpipe);
199 assert(transfer);
200
201 ctx->pipe->transfer_destroy(ctx->pipe, transfer);
202 }
203
204 static void*
205 sp_mpeg12_transfer_map(struct pipe_video_context *vpipe,
206 struct pipe_transfer *transfer)
207 {
208 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
209
210 assert(vpipe);
211 assert(transfer);
212
213 return ctx->pipe->transfer_map(ctx->pipe, transfer);
214 }
215
216 static void
217 sp_mpeg12_transfer_flush_region(struct pipe_video_context *vpipe,
218 struct pipe_transfer *transfer,
219 const struct pipe_box *box)
220 {
221 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
222
223 assert(vpipe);
224 assert(transfer);
225 assert(box);
226
227 ctx->pipe->transfer_flush_region(ctx->pipe, transfer, box);
228 }
229
230 static void
231 sp_mpeg12_transfer_unmap(struct pipe_video_context *vpipe,
232 struct pipe_transfer *transfer)
233 {
234 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
235
236 assert(vpipe);
237 assert(transfer);
238
239 ctx->pipe->transfer_unmap(ctx->pipe, transfer);
240 }
241
242 static void
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,
248 const void *data,
249 unsigned stride,
250 unsigned slice_stride)
251 {
252 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
253
254 assert(vpipe);
255 assert(resource);
256 assert(box);
257 assert(data);
258 assert(ctx->pipe->transfer_inline_write);
259
260 ctx->pipe->transfer_inline_write(ctx->pipe, resource, subresource, usage,
261 box, data, stride, slice_stride);
262 }
263
264 static void
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)
276 {
277 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
278
279 assert(vpipe);
280 assert(src_surface);
281 assert(src_area);
282 assert(dst_surface);
283 assert(dst_area);
284
285 vl_compositor_render(&ctx->compositor, src_surface,
286 picture_type, src_area, dst_surface, dst_area, fence);
287 }
288
289 static void
290 sp_mpeg12_set_picture_background(struct pipe_video_context *vpipe,
291 struct pipe_surface *bg,
292 struct pipe_video_rect *bg_src_rect)
293 {
294 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
295
296 assert(vpipe);
297 assert(bg);
298 assert(bg_src_rect);
299
300 vl_compositor_set_background(&ctx->compositor, bg, bg_src_rect);
301 }
302
303 static void
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[],
308 unsigned num_layers)
309 {
310 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
311
312 assert(vpipe);
313 assert((layers && src_rects && dst_rects) ||
314 (!layers && !src_rects && !dst_rects));
315
316 vl_compositor_set_layers(&ctx->compositor, layers, src_rects, dst_rects, num_layers);
317 }
318
319 static void
320 sp_mpeg12_set_decode_target(struct pipe_video_context *vpipe,
321 struct pipe_surface *dt)
322 {
323 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
324
325 assert(vpipe);
326 assert(dt);
327
328 pipe_surface_reference(&ctx->decode_target, dt);
329 }
330
331 static void
332 sp_mpeg12_set_csc_matrix(struct pipe_video_context *vpipe, const float *mat)
333 {
334 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
335
336 assert(vpipe);
337
338 vl_compositor_set_csc_matrix(&ctx->compositor, mat);
339 }
340
341 static bool
342 init_pipe_state(struct sp_mpeg12_context *ctx)
343 {
344 struct pipe_rasterizer_state rast;
345 struct pipe_blend_state blend;
346 struct pipe_depth_stencil_alpha_state dsa;
347 unsigned i;
348
349 assert(ctx);
350
351 memset(&rast, 0, sizeof rast);
352 rast.flatshade = 1;
353 rast.flatshade_first = 0;
354 rast.light_twoside = 0;
355 rast.front_ccw = 1;
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;
361 rast.scissor = 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;
372 rast.line_width = 1;
373 rast.point_smooth = 0;
374 rast.point_quad_rasterization = 0;
375 rast.point_size = 1;
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);
381
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;
395 blend.dither = 0;
396 ctx->blend = ctx->pipe->create_blend_state(ctx->pipe, &blend);
397 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend);
398
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;
411 }
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);
417
418 return true;
419 }
420
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,
427 bool pot_buffers,
428 enum pipe_format decode_format)
429 {
430 struct sp_mpeg12_context *ctx;
431
432 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
433
434 ctx = CALLOC_STRUCT(sp_mpeg12_context);
435
436 if (!ctx)
437 return NULL;
438
439 ctx->base.profile = profile;
440 ctx->base.chroma_format = chroma_format;
441 ctx->base.width = width;
442 ctx->base.height = height;
443
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;
463
464 ctx->pipe = pipe;
465 ctx->decode_format = decode_format;
466
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);
471 FREE(ctx);
472 return NULL;
473 }
474
475 if (!vl_compositor_init(&ctx->compositor, ctx->pipe)) {
476 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
477 ctx->pipe->destroy(ctx->pipe);
478 FREE(ctx);
479 return NULL;
480 }
481
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);
486 FREE(ctx);
487 return NULL;
488 }
489
490 return &ctx->base;
491 }
492
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)
497 {
498 struct pipe_context *pipe;
499
500 assert(screen);
501 assert(width && height);
502
503 pipe = screen->context_create(screen, NULL);
504 if (!pipe)
505 return NULL;
506
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,
510 chroma_format,
511 width, height,
512 VL_MPEG12_MC_RENDERER_BUFFER_PICTURE,
513 VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE,
514 true,
515 PIPE_FORMAT_XYUV);
516 }
517
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,
524 bool pot_buffers,
525 enum pipe_format decode_format)
526 {
527 assert(pipe);
528 assert(width && height);
529
530 switch (u_reduce_video_profile(profile)) {
531 case PIPE_VIDEO_CODEC_MPEG12:
532 return sp_mpeg12_create(pipe, profile,
533 chroma_format,
534 width, height,
535 bufmode, eb_handling,
536 pot_buffers,
537 decode_format);
538 default:
539 return NULL;
540 }
541 }