[g3dvl] make resource format selection a public interface
[mesa.git] / src / gallium / state_trackers / xorg / xvmc / surface.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 <assert.h>
29 #include <stdio.h>
30 #include <X11/Xlibint.h>
31 #include <vl_winsys.h>
32 #include <pipe/p_video_context.h>
33 #include <pipe/p_video_state.h>
34 #include <pipe/p_state.h>
35 #include <util/u_inlines.h>
36 #include <util/u_memory.h>
37 #include <util/u_math.h>
38 #include "xvmc_private.h"
39
40 static enum pipe_mpeg12_picture_type PictureToPipe(int xvmc_pic)
41 {
42 switch (xvmc_pic) {
43 case XVMC_TOP_FIELD:
44 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP;
45 case XVMC_BOTTOM_FIELD:
46 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM;
47 case XVMC_FRAME_PICTURE:
48 return PIPE_MPEG12_PICTURE_TYPE_FRAME;
49 default:
50 assert(0);
51 }
52
53 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized picture type 0x%08X.\n", xvmc_pic);
54
55 return -1;
56 }
57
58 static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, unsigned xvmc_picture_structure)
59 {
60 switch (xvmc_motion_type) {
61 case XVMC_PREDICTION_FRAME:
62 if (xvmc_picture_structure == XVMC_FRAME_PICTURE)
63 return PIPE_MPEG12_MOTION_TYPE_FRAME;
64 else
65 return PIPE_MPEG12_MOTION_TYPE_16x8;
66 break;
67
68 case XVMC_PREDICTION_FIELD:
69 return PIPE_MPEG12_MOTION_TYPE_FIELD;
70
71 case XVMC_PREDICTION_DUAL_PRIME:
72 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME;
73 }
74
75 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized motion type 0x%08X (with picture structure 0x%08X).\n", xvmc_motion_type, xvmc_picture_structure);
76
77 return -1;
78 }
79
80 static void
81 MacroBlocksToPipe(struct pipe_screen *screen,
82 unsigned int xvmc_picture_structure,
83 const XvMCMacroBlockArray *xvmc_macroblocks,
84 const XvMCBlockArray *xvmc_blocks,
85 unsigned int first_macroblock,
86 unsigned int num_macroblocks,
87 struct pipe_mpeg12_macroblock *mb)
88 {
89 unsigned int i, j;
90 XvMCMacroBlock *xvmc_mb;
91
92 assert(xvmc_macroblocks);
93 assert(xvmc_blocks);
94 assert(mb);
95 assert(num_macroblocks);
96
97 xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
98
99 for (i = 0; i < num_macroblocks; ++i) {
100 mb->base.codec = PIPE_VIDEO_CODEC_MPEG12;
101 mb->mbx = xvmc_mb->x;
102 mb->mby = xvmc_mb->y;
103
104 if (!(xvmc_mb->macroblock_type & XVMC_MB_TYPE_INTRA))
105 mb->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_picture_structure);
106 /* Get rid of Valgrind 'undefined' warnings */
107 else
108 mb->mo_type = -1;
109
110 mb->dct_intra = xvmc_mb->macroblock_type & XVMC_MB_TYPE_INTRA;
111 mb->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
112 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
113
114 switch (xvmc_mb->macroblock_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) {
115 case XVMC_MB_TYPE_MOTION_FORWARD:
116 mb->mv[0].wheight = 255;
117 mb->mv[1].wheight = 0;
118 break;
119
120 case (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD):
121 mb->mv[0].wheight = 127;
122 mb->mv[1].wheight = 127;
123 break;
124
125 case XVMC_MB_TYPE_MOTION_BACKWARD:
126 mb->mv[0].wheight = 0;
127 mb->mv[1].wheight = 255;
128 break;
129
130 default:
131 /* workaround for xines xxmc video out plugin */
132 if (!(xvmc_mb->macroblock_type & ~XVMC_MB_TYPE_PATTERN)) {
133 mb->mv[0].wheight = 255;
134 mb->mv[1].wheight = 0;
135 } else {
136 mb->mv[0].wheight = 0;
137 mb->mv[1].wheight = 0;
138 }
139 break;
140 }
141
142 for (j = 0; j < 2; ++j) {
143 mb->mv[j].top.x = xvmc_mb->PMV[0][j][0];
144 mb->mv[j].top.y = xvmc_mb->PMV[0][j][1];
145 mb->mv[j].bottom.x = xvmc_mb->PMV[1][j][0];
146 mb->mv[j].bottom.y = xvmc_mb->PMV[1][j][1];
147 }
148
149 mb->mv[0].top.field_select = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_FIRST_FORWARD;
150 mb->mv[1].top.field_select = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_FIRST_BACKWARD;
151 mb->mv[0].bottom.field_select = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_SECOND_FORWARD;
152 mb->mv[1].bottom.field_select = xvmc_mb->motion_vertical_field_select & XVMC_SELECT_SECOND_BACKWARD;
153
154 mb->cbp = xvmc_mb->coded_block_pattern;
155 mb->blocks = xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES;
156
157 ++mb;
158 ++xvmc_mb;
159 }
160 }
161
162 static void
163 unmap_and_flush_surface(XvMCSurfacePrivate *surface)
164 {
165 struct pipe_video_buffer *ref_frames[2];
166 XvMCContextPrivate *context_priv;
167 unsigned i;
168
169 assert(surface);
170
171 context_priv = surface->context->privData;
172
173 for ( i = 0; i < 2; ++i ) {
174 if (surface->ref_surfaces[i]) {
175 XvMCSurfacePrivate *ref = surface->ref_surfaces[i]->privData;
176
177 assert(ref);
178
179 unmap_and_flush_surface(ref);
180 surface->ref_surfaces[i] = NULL;
181 ref_frames[i] = ref->video_buffer;
182 } else {
183 ref_frames[i] = NULL;
184 }
185 }
186
187 if (surface->mapped) {
188 surface->decode_buffer->unmap(surface->decode_buffer);
189 context_priv->decoder->flush_buffer(surface->decode_buffer,
190 ref_frames,
191 surface->video_buffer,
192 &surface->flush_fence);
193 surface->mapped = 0;
194 }
195 }
196
197 PUBLIC
198 Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
199 {
200 const enum pipe_format resource_formats[3] = {
201 PIPE_FORMAT_R8_SNORM,
202 PIPE_FORMAT_R8_SNORM,
203 PIPE_FORMAT_R8_SNORM
204 };
205
206 XvMCContextPrivate *context_priv;
207 struct pipe_video_context *vpipe;
208 XvMCSurfacePrivate *surface_priv;
209
210 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating surface %p.\n", surface);
211
212 assert(dpy);
213
214 if (!context)
215 return XvMCBadContext;
216 if (!surface)
217 return XvMCBadSurface;
218
219 context_priv = context->privData;
220 vpipe = context_priv->vctx->vpipe;
221
222 surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
223 if (!surface_priv)
224 return BadAlloc;
225
226 surface_priv->decode_buffer = context_priv->decoder->create_buffer(context_priv->decoder);
227 surface_priv->video_buffer = vpipe->create_buffer(vpipe, PIPE_FORMAT_YV12, //TODO
228 resource_formats,
229 context_priv->decoder->chroma_format,
230 context_priv->decoder->width,
231 context_priv->decoder->height);
232 surface_priv->context = context;
233
234 surface->surface_id = XAllocID(dpy);
235 surface->context_id = context->context_id;
236 surface->surface_type_id = context->surface_type_id;
237 surface->width = context->width;
238 surface->height = context->height;
239 surface->privData = surface_priv;
240
241 SyncHandle();
242
243 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p created.\n", surface);
244
245 return Success;
246 }
247
248 PUBLIC
249 Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
250 XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
251 unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
252 XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks
253 )
254 {
255 struct pipe_video_context *vpipe;
256 struct pipe_video_decode_buffer *t_buffer;
257 XvMCContextPrivate *context_priv;
258 XvMCSurfacePrivate *target_surface_priv;
259 XvMCSurfacePrivate *past_surface_priv;
260 XvMCSurfacePrivate *future_surface_priv;
261 struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
262
263 XVMC_MSG(XVMC_TRACE, "[XvMC] Rendering to surface %p, with past %p and future %p\n",
264 target_surface, past_surface, future_surface);
265
266 assert(dpy);
267
268 if (!context || !context->privData)
269 return XvMCBadContext;
270 if (!target_surface || !target_surface->privData)
271 return XvMCBadSurface;
272
273 if (picture_structure != XVMC_TOP_FIELD &&
274 picture_structure != XVMC_BOTTOM_FIELD &&
275 picture_structure != XVMC_FRAME_PICTURE)
276 return BadValue;
277 /* Bkwd pred equivalent to fwd (past && !future) */
278 if (future_surface && !past_surface)
279 return BadMatch;
280
281 assert(context->context_id == target_surface->context_id);
282 assert(!past_surface || context->context_id == past_surface->context_id);
283 assert(!future_surface || context->context_id == future_surface->context_id);
284
285 assert(macroblocks);
286 assert(blocks);
287
288 assert(macroblocks->context_id == context->context_id);
289 assert(blocks->context_id == context->context_id);
290
291 assert(flags == 0 || flags == XVMC_SECOND_FIELD);
292
293 target_surface_priv = target_surface->privData;
294 past_surface_priv = past_surface ? past_surface->privData : NULL;
295 future_surface_priv = future_surface ? future_surface->privData : NULL;
296
297 assert(target_surface_priv->context == context);
298 assert(!past_surface || past_surface_priv->context == context);
299 assert(!future_surface || future_surface_priv->context == context);
300
301 context_priv = context->privData;
302 vpipe = context_priv->vctx->vpipe;
303
304 t_buffer = target_surface_priv->decode_buffer;
305
306 // enshure that all reference frames are flushed
307 // not really nessasary, but speeds ups rendering
308 if (past_surface)
309 unmap_and_flush_surface(past_surface->privData);
310
311 if (future_surface)
312 unmap_and_flush_surface(future_surface->privData);
313
314 /* If the surface we're rendering hasn't changed the ref frames shouldn't change. */
315 if (target_surface_priv->mapped && (
316 target_surface_priv->ref_surfaces[0] != past_surface ||
317 target_surface_priv->ref_surfaces[1] != future_surface)) {
318
319 // If they change anyway we need to flush our surface
320 unmap_and_flush_surface(target_surface_priv);
321 }
322
323 MacroBlocksToPipe(vpipe->screen, picture_structure, macroblocks, blocks, first_macroblock,
324 num_macroblocks, pipe_macroblocks);
325
326 if (!target_surface_priv->mapped) {
327 t_buffer->map(t_buffer);
328 target_surface_priv->ref_surfaces[0] = past_surface;
329 target_surface_priv->ref_surfaces[1] = future_surface;
330 target_surface_priv->mapped = 1;
331 }
332
333 t_buffer->add_macroblocks(t_buffer, num_macroblocks, &pipe_macroblocks->base);
334
335 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for rendering.\n", target_surface);
336
337 return Success;
338 }
339
340 PUBLIC
341 Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
342 {
343 assert(dpy);
344
345 if (!surface)
346 return XvMCBadSurface;
347
348 // don't call flush here, because this is usually
349 // called once for every slice instead of every frame
350
351 XVMC_MSG(XVMC_TRACE, "[XvMC] Flushing surface %p\n", surface);
352
353 return Success;
354 }
355
356 PUBLIC
357 Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
358 {
359 assert(dpy);
360
361 if (!surface)
362 return XvMCBadSurface;
363
364 XVMC_MSG(XVMC_TRACE, "[XvMC] Syncing surface %p\n", surface);
365
366 return Success;
367 }
368
369 PUBLIC
370 Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
371 short srcx, short srcy, unsigned short srcw, unsigned short srch,
372 short destx, short desty, unsigned short destw, unsigned short desth,
373 int flags)
374 {
375 static int dump_window = -1;
376
377 struct pipe_video_context *vpipe;
378 struct pipe_video_compositor *compositor;
379
380 XvMCSurfacePrivate *surface_priv;
381 XvMCContextPrivate *context_priv;
382 XvMCSubpicturePrivate *subpicture_priv;
383 XvMCContext *context;
384 struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch};
385 struct pipe_video_rect dst_rect = {destx, desty, destw, desth};
386 struct pipe_surface *drawable_surface;
387
388 XVMC_MSG(XVMC_TRACE, "[XvMC] Displaying surface %p.\n", surface);
389
390 assert(dpy);
391
392 if (!surface || !surface->privData)
393 return XvMCBadSurface;
394
395 surface_priv = surface->privData;
396 context = surface_priv->context;
397 context_priv = context->privData;
398
399 drawable_surface = vl_drawable_surface_get(context_priv->vctx, drawable);
400 if (!drawable_surface)
401 return BadDrawable;
402
403 assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
404 assert(srcx + srcw - 1 < surface->width);
405 assert(srcy + srch - 1 < surface->height);
406 /*
407 * Some apps (mplayer) hit these asserts because they call
408 * this function after the window has been resized by the WM
409 * but before they've handled the corresponding XEvent and
410 * know about the new dimensions. The output should be clipped
411 * until the app updates destw and desth.
412 */
413 /*
414 assert(destx + destw - 1 < drawable_surface->width);
415 assert(desty + desth - 1 < drawable_surface->height);
416 */
417
418 subpicture_priv = surface_priv->subpicture ? surface_priv->subpicture->privData : NULL;
419 vpipe = context_priv->vctx->vpipe;
420 compositor = context_priv->compositor;
421
422 unmap_and_flush_surface(surface_priv);
423
424 compositor->clear_layers(compositor);
425 compositor->set_buffer_layer(compositor, 0, surface_priv->video_buffer, &src_rect, NULL);
426
427 if (subpicture_priv) {
428 struct pipe_video_rect src_rect = {surface_priv->subx, surface_priv->suby, surface_priv->subw, surface_priv->subh};
429 struct pipe_video_rect dst_rect = {surface_priv->surfx, surface_priv->surfy, surface_priv->surfw, surface_priv->surfh};
430
431 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p has subpicture %p.\n", surface, surface_priv->subpicture);
432
433 assert(subpicture_priv->surface == surface);
434 if (subpicture_priv->palette)
435 compositor->set_palette_layer(compositor, 1, subpicture_priv->sampler, subpicture_priv->palette, &src_rect, &dst_rect);
436 else
437 compositor->set_rgba_layer(compositor, 1, subpicture_priv->sampler, &src_rect, &dst_rect);
438
439 surface_priv->subpicture = NULL;
440 subpicture_priv->surface = NULL;
441 }
442
443 compositor->render_picture(compositor, PictureToPipe(flags), drawable_surface, &dst_rect, &surface_priv->disp_fence);
444
445 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface);
446
447 vpipe->screen->flush_frontbuffer
448 (
449 vpipe->screen,
450 drawable_surface->texture,
451 0, 0,
452 vl_contextprivate_get(context_priv->vctx, drawable_surface)
453 );
454
455 pipe_surface_reference(&drawable_surface, NULL);
456
457 if(dump_window == -1) {
458 dump_window = debug_get_num_option("XVMC_DUMP", 0);
459 }
460
461 if(dump_window) {
462 static unsigned int framenum = 0;
463 char cmd[256];
464 sprintf(cmd, "xwd -id %d -out xvmc_frame_%08d.xwd", (int)drawable, ++framenum);
465 system(cmd);
466 }
467
468 XVMC_MSG(XVMC_TRACE, "[XvMC] Pushed surface %p to front buffer.\n", surface);
469
470 return Success;
471 }
472
473 PUBLIC
474 Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
475 {
476 assert(dpy);
477
478 if (!surface)
479 return XvMCBadSurface;
480
481 assert(status);
482
483 *status = 0;
484
485 return Success;
486 }
487
488 PUBLIC
489 Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
490 {
491 XvMCSurfacePrivate *surface_priv;
492
493 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying surface %p.\n", surface);
494
495 assert(dpy);
496
497 if (!surface || !surface->privData)
498 return XvMCBadSurface;
499
500 surface_priv = surface->privData;
501 surface_priv->decode_buffer->destroy(surface_priv->decode_buffer);
502 surface_priv->video_buffer->destroy(surface_priv->video_buffer);
503 FREE(surface_priv);
504 surface->privData = NULL;
505
506 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p destroyed.\n", surface);
507
508 return Success;
509 }
510
511 PUBLIC
512 Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
513 {
514 assert(dpy);
515
516 if (!surface || !surface->privData)
517 return XvMCBadSurface;
518
519 /* No op, only for overlaid rendering */
520
521 return Success;
522 }