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