vl: initial implementation of vlVaQueryImageFormats(), vlVaCreateImage(), vlVaQuerySu...
[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
102 return ctx->pipe->screen->is_format_supported(ctx->pipe->screen, format, PIPE_TEXTURE_2D,
103 0, usage, geom);
104 }
105
106 static void
107 sp_mpeg12_decode_macroblocks(struct pipe_video_context *vpipe,
108 struct pipe_surface *past,
109 struct pipe_surface *future,
110 unsigned num_macroblocks,
111 struct pipe_macroblock *macroblocks,
112 struct pipe_fence_handle **fence)
113 {
114 struct sp_mpeg12_context *ctx = (struct sp_mpeg12_context*)vpipe;
115 struct pipe_mpeg12_macroblock *mpeg12_macroblocks = (struct pipe_mpeg12_macroblock*)macroblocks;
116
117 assert(vpipe);
118 assert(num_macroblocks);
119 assert(macroblocks);
120 assert(macroblocks->codec == PIPE_VIDEO_CODEC_MPEG12);
121 assert(ctx->decode_target);
122
123 vl_mpeg12_mc_renderer_render_macroblocks(&ctx->mc_renderer,
124 ctx->decode_target,
125 past, future, num_macroblocks,
126 mpeg12_macroblocks, fence);
127 }
128
129 static void
130 sp_mpeg12_clear_render_target(struct pipe_video_context *vpipe,
131 struct pipe_surface *dst,
132 unsigned dstx, unsigned dsty,
133 unsigned width, unsigned height)
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_resource_copy_region(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_per_vertex = 1;
376 rast.offset_units = 1;
377 rast.offset_scale = 1;
378 rast.gl_rasterization_rules = 1;
379
380 ctx->rast = ctx->pipe->create_rasterizer_state(ctx->pipe, &rast);
381 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rast);
382
383 memset(&blend, 0, sizeof blend);
384
385 blend.independent_blend_enable = 0;
386 blend.rt[0].blend_enable = 0;
387 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
388 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
389 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ONE;
390 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
391 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
392 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ONE;
393 blend.logicop_enable = 0;
394 blend.logicop_func = PIPE_LOGICOP_CLEAR;
395 /* Needed to allow color writes to FB, even if blending disabled */
396 blend.rt[0].colormask = PIPE_MASK_RGBA;
397 blend.dither = 0;
398 ctx->blend = ctx->pipe->create_blend_state(ctx->pipe, &blend);
399 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend);
400
401 memset(&dsa, 0, sizeof dsa);
402 dsa.depth.enabled = 0;
403 dsa.depth.writemask = 0;
404 dsa.depth.func = PIPE_FUNC_ALWAYS;
405 for (i = 0; i < 2; ++i) {
406 dsa.stencil[i].enabled = 0;
407 dsa.stencil[i].func = PIPE_FUNC_ALWAYS;
408 dsa.stencil[i].fail_op = PIPE_STENCIL_OP_KEEP;
409 dsa.stencil[i].zpass_op = PIPE_STENCIL_OP_KEEP;
410 dsa.stencil[i].zfail_op = PIPE_STENCIL_OP_KEEP;
411 dsa.stencil[i].valuemask = 0;
412 dsa.stencil[i].writemask = 0;
413 }
414 dsa.alpha.enabled = 0;
415 dsa.alpha.func = PIPE_FUNC_ALWAYS;
416 dsa.alpha.ref_value = 0;
417 ctx->dsa = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &dsa);
418 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->dsa);
419
420 return true;
421 }
422
423 static struct pipe_video_context *
424 sp_mpeg12_create(struct pipe_context *pipe, enum pipe_video_profile profile,
425 enum pipe_video_chroma_format chroma_format,
426 unsigned width, unsigned height,
427 enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode,
428 enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling,
429 bool pot_buffers,
430 enum pipe_format decode_format)
431 {
432 struct sp_mpeg12_context *ctx;
433
434 assert(u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12);
435
436 ctx = CALLOC_STRUCT(sp_mpeg12_context);
437
438 if (!ctx)
439 return NULL;
440
441 ctx->base.profile = profile;
442 ctx->base.chroma_format = chroma_format;
443 ctx->base.width = width;
444 ctx->base.height = height;
445
446 ctx->base.screen = pipe->screen;
447
448 ctx->base.destroy = sp_mpeg12_destroy;
449 ctx->base.get_param = sp_mpeg12_get_param;
450 ctx->base.is_format_supported = sp_mpeg12_is_format_supported;
451 ctx->base.decode_macroblocks = sp_mpeg12_decode_macroblocks;
452 ctx->base.render_picture = sp_mpeg12_render_picture;
453 ctx->base.clear_render_target = sp_mpeg12_clear_render_target;
454 ctx->base.resource_copy_region = sp_mpeg12_resource_copy_region;
455 ctx->base.get_transfer = sp_mpeg12_get_transfer;
456 ctx->base.transfer_destroy = sp_mpeg12_transfer_destroy;
457 ctx->base.transfer_map = sp_mpeg12_transfer_map;
458 ctx->base.transfer_flush_region = sp_mpeg12_transfer_flush_region;
459 ctx->base.transfer_unmap = sp_mpeg12_transfer_unmap;
460 if (pipe->transfer_inline_write)
461 ctx->base.transfer_inline_write = sp_mpeg12_transfer_inline_write;
462 ctx->base.set_picture_background = sp_mpeg12_set_picture_background;
463 ctx->base.set_picture_layers = sp_mpeg12_set_picture_layers;
464 ctx->base.set_decode_target = sp_mpeg12_set_decode_target;
465 ctx->base.set_csc_matrix = sp_mpeg12_set_csc_matrix;
466
467 ctx->pipe = pipe;
468 ctx->decode_format = decode_format;
469
470 if (!vl_mpeg12_mc_renderer_init(&ctx->mc_renderer, ctx->pipe,
471 width, height, chroma_format,
472 bufmode, eb_handling, pot_buffers)) {
473 ctx->pipe->destroy(ctx->pipe);
474 FREE(ctx);
475 return NULL;
476 }
477
478 if (!vl_compositor_init(&ctx->compositor, ctx->pipe)) {
479 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
480 ctx->pipe->destroy(ctx->pipe);
481 FREE(ctx);
482 return NULL;
483 }
484
485 if (!init_pipe_state(ctx)) {
486 vl_compositor_cleanup(&ctx->compositor);
487 vl_mpeg12_mc_renderer_cleanup(&ctx->mc_renderer);
488 ctx->pipe->destroy(ctx->pipe);
489 FREE(ctx);
490 return NULL;
491 }
492
493 return &ctx->base;
494 }
495
496 struct pipe_video_context *
497 sp_video_create(struct pipe_screen *screen, enum pipe_video_profile profile,
498 enum pipe_video_chroma_format chroma_format,
499 unsigned width, unsigned height, void *priv)
500 {
501 struct pipe_context *pipe;
502
503 assert(screen);
504 assert(width && height);
505
506 pipe = screen->context_create(screen, NULL);
507 if (!pipe)
508 return NULL;
509
510 /* TODO: Use slice buffering for softpipe when implemented, no advantage to buffering an entire picture with softpipe */
511 /* TODO: Use XFER_NONE when implemented */
512 return sp_video_create_ex(pipe, profile,
513 chroma_format,
514 width, height,
515 VL_MPEG12_MC_RENDERER_BUFFER_PICTURE,
516 VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE,
517 true,
518 PIPE_FORMAT_XYUV);
519 }
520
521 struct pipe_video_context *
522 sp_video_create_ex(struct pipe_context *pipe, enum pipe_video_profile profile,
523 enum pipe_video_chroma_format chroma_format,
524 unsigned width, unsigned height,
525 enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode,
526 enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling,
527 bool pot_buffers,
528 enum pipe_format decode_format)
529 {
530 assert(pipe);
531 assert(width && height);
532
533 switch (u_reduce_video_profile(profile)) {
534 case PIPE_VIDEO_CODEC_MPEG12:
535 return sp_mpeg12_create(pipe, profile,
536 chroma_format,
537 width, height,
538 bufmode, eb_handling,
539 pot_buffers,
540 decode_format);
541 default:
542 return NULL;
543 }
544 }