db630c9e790f422524226c090db37399dfb1296f
[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 <X11/Xlibint.h>
30 #include <vl_winsys.h>
31 #include <pipe/p_video_context.h>
32 #include <pipe/p_video_state.h>
33 #include <pipe/p_state.h>
34 #include <util/u_inlines.h>
35 #include <util/u_memory.h>
36 #include "xvmc_private.h"
37
38 static enum pipe_mpeg12_macroblock_type TypeToPipe(int xvmc_mb_type)
39 {
40 if (xvmc_mb_type & XVMC_MB_TYPE_INTRA)
41 return PIPE_MPEG12_MACROBLOCK_TYPE_INTRA;
42 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_FORWARD)
43 return PIPE_MPEG12_MACROBLOCK_TYPE_FWD;
44 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == XVMC_MB_TYPE_MOTION_BACKWARD)
45 return PIPE_MPEG12_MACROBLOCK_TYPE_BKWD;
46 if ((xvmc_mb_type & (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD)) == (XVMC_MB_TYPE_MOTION_FORWARD | XVMC_MB_TYPE_MOTION_BACKWARD))
47 return PIPE_MPEG12_MACROBLOCK_TYPE_BI;
48
49 assert(0);
50
51 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized mb type 0x%08X.\n", xvmc_mb_type);
52
53 return -1;
54 }
55
56 static enum pipe_mpeg12_picture_type PictureToPipe(int xvmc_pic)
57 {
58 switch (xvmc_pic) {
59 case XVMC_TOP_FIELD:
60 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP;
61 case XVMC_BOTTOM_FIELD:
62 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM;
63 case XVMC_FRAME_PICTURE:
64 return PIPE_MPEG12_PICTURE_TYPE_FRAME;
65 default:
66 assert(0);
67 }
68
69 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized picture type 0x%08X.\n", xvmc_pic);
70
71 return -1;
72 }
73
74 static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, int xvmc_dct_type)
75 {
76 switch (xvmc_motion_type) {
77 case XVMC_PREDICTION_FRAME:
78 if (xvmc_dct_type == XVMC_DCT_TYPE_FIELD)
79 return PIPE_MPEG12_MOTION_TYPE_16x8;
80 else if (xvmc_dct_type == XVMC_DCT_TYPE_FRAME)
81 return PIPE_MPEG12_MOTION_TYPE_FRAME;
82 break;
83 case XVMC_PREDICTION_FIELD:
84 return PIPE_MPEG12_MOTION_TYPE_FIELD;
85 case XVMC_PREDICTION_DUAL_PRIME:
86 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME;
87 default:
88 assert(0);
89 }
90
91 XVMC_MSG(XVMC_ERR, "[XvMC] Unrecognized motion type 0x%08X (with DCT type 0x%08X).\n", xvmc_motion_type, xvmc_dct_type);
92
93 return -1;
94 }
95
96 static bool
97 CreateOrResizeBackBuffer(struct vl_context *vctx, unsigned int width, unsigned int height,
98 struct pipe_surface **backbuffer)
99 {
100 struct pipe_video_context *vpipe;
101 struct pipe_texture template;
102 struct pipe_texture *tex;
103
104 assert(vctx);
105
106 vpipe = vctx->vpipe;
107
108 if (*backbuffer) {
109 if ((*backbuffer)->width != width || (*backbuffer)->height != height)
110 pipe_surface_reference(backbuffer, NULL);
111 else
112 return true;
113 }
114
115 memset(&template, 0, sizeof(struct pipe_texture));
116 template.target = PIPE_TEXTURE_2D;
117 template.format = vctx->vscreen->format;
118 template.last_level = 0;
119 template.width0 = width;
120 template.height0 = height;
121 template.depth0 = 1;
122 template.tex_usage = PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
123
124 tex = vpipe->screen->texture_create(vpipe->screen, &template);
125 if (!tex)
126 return false;
127
128 *backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
129 PIPE_BUFFER_USAGE_GPU_READ |
130 PIPE_BUFFER_USAGE_GPU_WRITE);
131 pipe_texture_reference(&tex, NULL);
132
133 if (!*backbuffer)
134 return false;
135
136 /* Clear the backbuffer in case the video doesn't cover the whole window */
137 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
138 vpipe->surface_fill(vpipe, *backbuffer, 0, 0, width, height, 0);
139
140 return true;
141 }
142
143 static void
144 MacroBlocksToPipe(struct pipe_screen *screen,
145 const XvMCMacroBlockArray *xvmc_macroblocks,
146 const XvMCBlockArray *xvmc_blocks,
147 unsigned int first_macroblock,
148 unsigned int num_macroblocks,
149 struct pipe_mpeg12_macroblock *pipe_macroblocks)
150 {
151 unsigned int i, j, k, l;
152 XvMCMacroBlock *xvmc_mb;
153
154 assert(xvmc_macroblocks);
155 assert(xvmc_blocks);
156 assert(pipe_macroblocks);
157 assert(num_macroblocks);
158
159 xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
160
161 for (i = 0; i < num_macroblocks; ++i) {
162 pipe_macroblocks->base.codec = PIPE_VIDEO_CODEC_MPEG12;
163 pipe_macroblocks->mbx = xvmc_mb->x;
164 pipe_macroblocks->mby = xvmc_mb->y;
165 pipe_macroblocks->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
166 if (pipe_macroblocks->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
167 pipe_macroblocks->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_mb->dct_type);
168 /* Get rid of Valgrind 'undefined' warnings */
169 else
170 pipe_macroblocks->mo_type = -1;
171 pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
172 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
173
174 for (j = 0; j < 2; ++j)
175 for (k = 0; k < 2; ++k)
176 for (l = 0; l < 2; ++l)
177 pipe_macroblocks->pmv[j][k][l] = xvmc_mb->PMV[j][k][l];
178
179 pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
180 pipe_macroblocks->blocks = pipe_user_buffer_create(screen, xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES,
181 BLOCK_SIZE_BYTES);
182
183 ++pipe_macroblocks;
184 ++xvmc_mb;
185 }
186 }
187
188 Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
189 {
190 XvMCContextPrivate *context_priv;
191 struct pipe_video_context *vpipe;
192 XvMCSurfacePrivate *surface_priv;
193 struct pipe_video_surface *vsfc;
194
195 XVMC_MSG(XVMC_TRACE, "[XvMC] Creating surface %p.\n", surface);
196
197 assert(dpy);
198
199 if (!context)
200 return XvMCBadContext;
201 if (!surface)
202 return XvMCBadSurface;
203
204 context_priv = context->privData;
205 vpipe = context_priv->vctx->vpipe;
206
207 surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
208 if (!surface_priv)
209 return BadAlloc;
210
211 assert(vpipe->screen->video_surface_create);
212 vsfc = vpipe->screen->video_surface_create(vpipe->screen, vpipe->chroma_format,
213 vpipe->width, vpipe->height);
214 if (!vsfc) {
215 FREE(surface_priv);
216 return BadAlloc;
217 }
218
219 surface_priv->pipe_vsfc = vsfc;
220 surface_priv->context = context;
221
222 surface->surface_id = XAllocID(dpy);
223 surface->context_id = context->context_id;
224 surface->surface_type_id = context->surface_type_id;
225 surface->width = context->width;
226 surface->height = context->height;
227 surface->privData = surface_priv;
228
229 SyncHandle();
230
231 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p created.\n", surface);
232
233 return Success;
234 }
235
236 Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
237 XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
238 unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
239 XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks
240 )
241 {
242 struct pipe_video_context *vpipe;
243 struct pipe_surface *t_vsfc;
244 struct pipe_surface *p_vsfc;
245 struct pipe_surface *f_vsfc;
246 XvMCContextPrivate *context_priv;
247 XvMCSurfacePrivate *target_surface_priv;
248 XvMCSurfacePrivate *past_surface_priv;
249 XvMCSurfacePrivate *future_surface_priv;
250 struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
251 unsigned int i;
252
253 XVMC_MSG(XVMC_TRACE, "[XvMC] Rendering to surface %p.\n", target_surface);
254
255 assert(dpy);
256
257 if (!context || !context->privData)
258 return XvMCBadContext;
259 if (!target_surface || !target_surface->privData)
260 return XvMCBadSurface;
261
262 if (picture_structure != XVMC_TOP_FIELD &&
263 picture_structure != XVMC_BOTTOM_FIELD &&
264 picture_structure != XVMC_FRAME_PICTURE)
265 return BadValue;
266 /* Bkwd pred equivalent to fwd (past && !future) */
267 if (future_surface && !past_surface)
268 return BadMatch;
269
270 assert(context->context_id == target_surface->context_id);
271 assert(!past_surface || context->context_id == past_surface->context_id);
272 assert(!future_surface || context->context_id == future_surface->context_id);
273
274 assert(macroblocks);
275 assert(blocks);
276
277 assert(macroblocks->context_id == context->context_id);
278 assert(blocks->context_id == context->context_id);
279
280 assert(flags == 0 || flags == XVMC_SECOND_FIELD);
281
282 target_surface_priv = target_surface->privData;
283 past_surface_priv = past_surface ? past_surface->privData : NULL;
284 future_surface_priv = future_surface ? future_surface->privData : NULL;
285
286 assert(target_surface_priv->context == context);
287 assert(!past_surface || past_surface_priv->context == context);
288 assert(!future_surface || future_surface_priv->context == context);
289
290 context_priv = context->privData;
291 vpipe = context_priv->vctx->vpipe;
292
293 t_vsfc = target_surface_priv->pipe_vsfc;
294 p_vsfc = past_surface ? past_surface_priv->pipe_vsfc : NULL;
295 f_vsfc = future_surface ? future_surface_priv->pipe_vsfc : NULL;
296
297 MacroBlocksToPipe(vpipe->screen, macroblocks, blocks, first_macroblock,
298 num_macroblocks, pipe_macroblocks);
299
300 vpipe->set_decode_target(vpipe, t_vsfc);
301 vpipe->decode_macroblocks(vpipe, p_vsfc, f_vsfc, num_macroblocks,
302 &pipe_macroblocks->base, target_surface_priv->render_fence);
303
304 for (i = 0; i < num_macroblocks; ++i)
305 vpipe->screen->buffer_destroy(pipe_macroblocks[i].blocks);
306
307 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for rendering.\n", target_surface);
308
309 return Success;
310 }
311
312 Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
313 {
314 assert(dpy);
315
316 if (!surface)
317 return XvMCBadSurface;
318
319 return Success;
320 }
321
322 Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
323 {
324 assert(dpy);
325
326 if (!surface)
327 return XvMCBadSurface;
328
329 return Success;
330 }
331
332 Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
333 short srcx, short srcy, unsigned short srcw, unsigned short srch,
334 short destx, short desty, unsigned short destw, unsigned short desth,
335 int flags)
336 {
337 Window root;
338 int x, y;
339 unsigned int width, height;
340 unsigned int border_width;
341 unsigned int depth;
342 struct pipe_video_context *vpipe;
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 if (XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
358 return BadDrawable;
359
360 assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
361 assert(srcx + srcw - 1 < surface->width);
362 assert(srcy + srch - 1 < surface->height);
363 /*
364 * Some apps (mplayer) hit these asserts because they call
365 * this function after the window has been resized by the WM
366 * but before they've handled the corresponding XEvent and
367 * know about the new dimensions. The output should be clipped
368 * until the app updates destw and desth.
369 */
370 /*
371 assert(destx + destw - 1 < width);
372 assert(desty + desth - 1 < height);
373 */
374
375 surface_priv = surface->privData;
376 context = surface_priv->context;
377 context_priv = context->privData;
378 subpicture_priv = surface_priv->subpicture ? surface_priv->subpicture->privData : NULL;
379 vpipe = context_priv->vctx->vpipe;
380
381 if (!CreateOrResizeBackBuffer(context_priv->vctx, width, height, &context_priv->backbuffer))
382 return BadAlloc;
383
384 if (subpicture_priv) {
385 struct pipe_video_rect src_rect = {surface_priv->subx, surface_priv->suby, surface_priv->subw, surface_priv->subh};
386 struct pipe_video_rect dst_rect = {surface_priv->surfx, surface_priv->surfy, surface_priv->surfw, surface_priv->surfh};
387 struct pipe_video_rect *src_rects[1] = {&src_rect};
388 struct pipe_video_rect *dst_rects[1] = {&dst_rect};
389
390 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p has subpicture %p.\n", surface, surface_priv->subpicture);
391
392 assert(subpicture_priv->surface == surface);
393 vpipe->set_picture_layers(vpipe, &subpicture_priv->sfc->texture, &src_rects, &dst_rects, 1);
394
395 surface_priv->subpicture = NULL;
396 subpicture_priv->surface = NULL;
397 }
398 else
399 vpipe->set_picture_layers(vpipe, NULL, NULL, NULL, 0);
400
401 vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
402 context_priv->backbuffer, &dst_rect, surface_priv->disp_fence);
403
404 XVMC_MSG(XVMC_TRACE, "[XvMC] Submitted surface %p for display. Pushing to front buffer.\n", surface);
405
406 vl_video_bind_drawable(context_priv->vctx, drawable);
407
408 vpipe->screen->flush_frontbuffer
409 (
410 vpipe->screen,
411 context_priv->backbuffer,
412 vpipe->priv
413 );
414
415 XVMC_MSG(XVMC_TRACE, "[XvMC] Pushed surface %p to front buffer.\n", surface);
416
417 return Success;
418 }
419
420 Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
421 {
422 assert(dpy);
423
424 if (!surface)
425 return XvMCBadSurface;
426
427 assert(status);
428
429 *status = 0;
430
431 return Success;
432 }
433
434 Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
435 {
436 XvMCSurfacePrivate *surface_priv;
437
438 XVMC_MSG(XVMC_TRACE, "[XvMC] Destroying surface %p.\n", surface);
439
440 assert(dpy);
441
442 if (!surface || !surface->privData)
443 return XvMCBadSurface;
444
445 surface_priv = surface->privData;
446 pipe_video_surface_reference(&surface_priv->pipe_vsfc, NULL);
447 FREE(surface_priv);
448 surface->privData = NULL;
449
450 XVMC_MSG(XVMC_TRACE, "[XvMC] Surface %p destroyed.\n", surface);
451
452 return Success;
453 }
454
455 Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
456 {
457 assert(dpy);
458
459 if (!surface || !surface->privData)
460 return XvMCBadSurface;
461
462 /* No op, only for overlaid rendering */
463
464 return Success;
465 }