g3dvl: It's ok to not have cliprects (minimized windows, etc).
[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 <pipe/p_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 return -1;
52 }
53
54 static enum pipe_mpeg12_picture_type PictureToPipe(int xvmc_pic)
55 {
56 switch (xvmc_pic) {
57 case XVMC_TOP_FIELD:
58 return PIPE_MPEG12_PICTURE_TYPE_FIELD_TOP;
59 case XVMC_BOTTOM_FIELD:
60 return PIPE_MPEG12_PICTURE_TYPE_FIELD_BOTTOM;
61 case XVMC_FRAME_PICTURE:
62 return PIPE_MPEG12_PICTURE_TYPE_FRAME;
63 default:
64 assert(0);
65 }
66
67 return -1;
68 }
69
70 static enum pipe_mpeg12_motion_type MotionToPipe(int xvmc_motion_type, int xvmc_dct_type)
71 {
72 switch (xvmc_motion_type) {
73 case XVMC_PREDICTION_FRAME:
74 return xvmc_dct_type == XVMC_DCT_TYPE_FIELD ?
75 PIPE_MPEG12_MOTION_TYPE_16x8 : PIPE_MPEG12_MOTION_TYPE_FRAME;
76 case XVMC_PREDICTION_FIELD:
77 return PIPE_MPEG12_MOTION_TYPE_FIELD;
78 case XVMC_PREDICTION_DUAL_PRIME:
79 return PIPE_MPEG12_MOTION_TYPE_DUALPRIME;
80 default:
81 assert(0);
82 }
83
84 return -1;
85 }
86
87 static bool
88 CreateOrResizeBackBuffer(struct vl_context *vctx, unsigned int width, unsigned int height,
89 struct pipe_surface **backbuffer)
90 {
91 struct pipe_video_context *vpipe;
92 struct pipe_texture template;
93 struct pipe_texture *tex;
94
95 assert(vctx);
96
97 vpipe = vctx->vpipe;
98
99 if (*backbuffer) {
100 if ((*backbuffer)->width != width || (*backbuffer)->height != height)
101 pipe_surface_reference(backbuffer, NULL);
102 else
103 return true;
104 }
105
106 memset(&template, 0, sizeof(struct pipe_texture));
107 template.target = PIPE_TEXTURE_2D;
108 template.format = vctx->vscreen->format;
109 template.last_level = 0;
110 template.width[0] = width;
111 template.height[0] = height;
112 template.depth[0] = 1;
113 pf_get_block(template.format, &template.block);
114 template.tex_usage = PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
115
116 tex = vpipe->screen->texture_create(vpipe->screen, &template);
117 if (!tex)
118 return false;
119
120 *backbuffer = vpipe->screen->get_tex_surface(vpipe->screen, tex, 0, 0, 0,
121 PIPE_BUFFER_USAGE_GPU_READ |
122 PIPE_BUFFER_USAGE_GPU_WRITE);
123 pipe_texture_reference(&tex, NULL);
124
125 if (!*backbuffer)
126 return false;
127
128 /* Clear the backbuffer in case the video doesn't cover the whole window */
129 /* FIXME: Need to clear every time a frame moves and leaves dirty rects */
130 vpipe->surface_fill(vpipe, *backbuffer, 0, 0, width, height, 0);
131
132 return true;
133 }
134
135 static void
136 MacroBlocksToPipe(struct pipe_screen *screen,
137 const XvMCMacroBlockArray *xvmc_macroblocks,
138 const XvMCBlockArray *xvmc_blocks,
139 unsigned int first_macroblock,
140 unsigned int num_macroblocks,
141 struct pipe_mpeg12_macroblock *pipe_macroblocks)
142 {
143 unsigned int i, j, k, l;
144 XvMCMacroBlock *xvmc_mb;
145
146 assert(xvmc_macroblocks);
147 assert(xvmc_blocks);
148 assert(pipe_macroblocks);
149 assert(num_macroblocks);
150
151 xvmc_mb = xvmc_macroblocks->macro_blocks + first_macroblock;
152
153 for (i = 0; i < num_macroblocks; ++i) {
154 pipe_macroblocks->base.codec = PIPE_VIDEO_CODEC_MPEG12;
155 pipe_macroblocks->mbx = xvmc_mb->x;
156 pipe_macroblocks->mby = xvmc_mb->y;
157 pipe_macroblocks->mb_type = TypeToPipe(xvmc_mb->macroblock_type);
158 if (pipe_macroblocks->mb_type != PIPE_MPEG12_MACROBLOCK_TYPE_INTRA)
159 pipe_macroblocks->mo_type = MotionToPipe(xvmc_mb->motion_type, xvmc_mb->dct_type);
160 /* Get rid of Valgrind 'undefined' warnings */
161 else
162 pipe_macroblocks->mo_type = -1;
163 pipe_macroblocks->dct_type = xvmc_mb->dct_type == XVMC_DCT_TYPE_FIELD ?
164 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
165
166 for (j = 0; j < 2; ++j)
167 for (k = 0; k < 2; ++k)
168 for (l = 0; l < 2; ++l)
169 pipe_macroblocks->pmv[j][k][l] = xvmc_mb->PMV[j][k][l];
170
171 pipe_macroblocks->cbp = xvmc_mb->coded_block_pattern;
172 pipe_macroblocks->blocks = pipe_user_buffer_create(screen, xvmc_blocks->blocks + xvmc_mb->index * BLOCK_SIZE_SAMPLES,
173 BLOCK_SIZE_BYTES);
174
175 ++pipe_macroblocks;
176 ++xvmc_mb;
177 }
178 }
179
180 Status XvMCCreateSurface(Display *dpy, XvMCContext *context, XvMCSurface *surface)
181 {
182 XvMCContextPrivate *context_priv;
183 struct pipe_video_context *vpipe;
184 XvMCSurfacePrivate *surface_priv;
185 struct pipe_video_surface *vsfc;
186
187 assert(dpy);
188
189 if (!context)
190 return XvMCBadContext;
191 if (!surface)
192 return XvMCBadSurface;
193
194 context_priv = context->privData;
195 vpipe = context_priv->vctx->vpipe;
196
197 surface_priv = CALLOC(1, sizeof(XvMCSurfacePrivate));
198 if (!surface_priv)
199 return BadAlloc;
200
201 vsfc = vpipe->screen->video_surface_create(vpipe->screen, vpipe->chroma_format,
202 vpipe->width, vpipe->height);
203 if (!vsfc) {
204 FREE(surface_priv);
205 return BadAlloc;
206 }
207
208 surface_priv->pipe_vsfc = vsfc;
209 surface_priv->context = context;
210
211 surface->surface_id = XAllocID(dpy);
212 surface->context_id = context->context_id;
213 surface->surface_type_id = context->surface_type_id;
214 surface->width = context->width;
215 surface->height = context->height;
216 surface->privData = surface_priv;
217
218 SyncHandle();
219
220 return Success;
221 }
222
223 Status XvMCRenderSurface(Display *dpy, XvMCContext *context, unsigned int picture_structure,
224 XvMCSurface *target_surface, XvMCSurface *past_surface, XvMCSurface *future_surface,
225 unsigned int flags, unsigned int num_macroblocks, unsigned int first_macroblock,
226 XvMCMacroBlockArray *macroblocks, XvMCBlockArray *blocks
227 )
228 {
229 struct pipe_video_context *vpipe;
230 struct pipe_surface *t_vsfc;
231 struct pipe_surface *p_vsfc;
232 struct pipe_surface *f_vsfc;
233 XvMCContextPrivate *context_priv;
234 XvMCSurfacePrivate *target_surface_priv;
235 XvMCSurfacePrivate *past_surface_priv;
236 XvMCSurfacePrivate *future_surface_priv;
237 struct pipe_mpeg12_macroblock pipe_macroblocks[num_macroblocks];
238 unsigned int i;
239
240 assert(dpy);
241
242 if (!context || !context->privData)
243 return XvMCBadContext;
244 if (!target_surface || !target_surface->privData)
245 return XvMCBadSurface;
246
247 if (picture_structure != XVMC_TOP_FIELD &&
248 picture_structure != XVMC_BOTTOM_FIELD &&
249 picture_structure != XVMC_FRAME_PICTURE)
250 return BadValue;
251 /* Bkwd pred equivalent to fwd (past && !future) */
252 if (future_surface && !past_surface)
253 return BadMatch;
254
255 assert(context->context_id == target_surface->context_id);
256 assert(!past_surface || context->context_id == past_surface->context_id);
257 assert(!future_surface || context->context_id == future_surface->context_id);
258
259 assert(macroblocks);
260 assert(blocks);
261
262 assert(macroblocks->context_id == context->context_id);
263 assert(blocks->context_id == context->context_id);
264
265 assert(flags == 0 || flags == XVMC_SECOND_FIELD);
266
267 target_surface_priv = target_surface->privData;
268 past_surface_priv = past_surface ? past_surface->privData : NULL;
269 future_surface_priv = future_surface ? future_surface->privData : NULL;
270
271 assert(target_surface_priv->context == context);
272 assert(!past_surface || past_surface_priv->context == context);
273 assert(!future_surface || future_surface_priv->context == context);
274
275 context_priv = context->privData;
276 vpipe = context_priv->vctx->vpipe;
277
278 t_vsfc = target_surface_priv->pipe_vsfc;
279 p_vsfc = past_surface ? past_surface_priv->pipe_vsfc : NULL;
280 f_vsfc = future_surface ? future_surface_priv->pipe_vsfc : NULL;
281
282 MacroBlocksToPipe(vpipe->screen, macroblocks, blocks, first_macroblock,
283 num_macroblocks, pipe_macroblocks);
284
285 vpipe->set_decode_target(vpipe, t_vsfc);
286 vpipe->decode_macroblocks(vpipe, p_vsfc, f_vsfc, num_macroblocks,
287 &pipe_macroblocks->base, target_surface_priv->render_fence);
288
289 for (i = 0; i < num_macroblocks; ++i)
290 vpipe->screen->buffer_destroy(pipe_macroblocks[i].blocks);
291
292 return Success;
293 }
294
295 Status XvMCFlushSurface(Display *dpy, XvMCSurface *surface)
296 {
297 assert(dpy);
298
299 if (!surface)
300 return XvMCBadSurface;
301
302 return Success;
303 }
304
305 Status XvMCSyncSurface(Display *dpy, XvMCSurface *surface)
306 {
307 assert(dpy);
308
309 if (!surface)
310 return XvMCBadSurface;
311
312 return Success;
313 }
314
315 Status XvMCPutSurface(Display *dpy, XvMCSurface *surface, Drawable drawable,
316 short srcx, short srcy, unsigned short srcw, unsigned short srch,
317 short destx, short desty, unsigned short destw, unsigned short desth,
318 int flags)
319 {
320 Window root;
321 int x, y;
322 unsigned int width, height;
323 unsigned int border_width;
324 unsigned int depth;
325 struct pipe_video_context *vpipe;
326 XvMCSurfacePrivate *surface_priv;
327 XvMCContextPrivate *context_priv;
328 XvMCContext *context;
329 struct pipe_video_rect src_rect = {srcx, srcy, srcw, srch};
330 struct pipe_video_rect dst_rect = {destx, desty, destw, desth};
331
332 assert(dpy);
333
334 if (!surface || !surface->privData)
335 return XvMCBadSurface;
336
337 if (XGetGeometry(dpy, drawable, &root, &x, &y, &width, &height, &border_width, &depth) == BadDrawable)
338 return BadDrawable;
339
340 assert(flags == XVMC_TOP_FIELD || flags == XVMC_BOTTOM_FIELD || flags == XVMC_FRAME_PICTURE);
341 assert(srcx + srcw - 1 < surface->width);
342 assert(srcy + srch - 1 < surface->height);
343 /*
344 * Some apps (mplayer) hit these asserts because they call
345 * this function after the window has been resized by the WM
346 * but before they've handled the corresponding XEvent and
347 * know about the new dimensions. The output should be clipped
348 * until the app updates destw and desth.
349 */
350 /*
351 assert(destx + destw - 1 < width);
352 assert(desty + desth - 1 < height);
353 */
354
355 surface_priv = surface->privData;
356 context = surface_priv->context;
357 context_priv = context->privData;
358 vpipe = context_priv->vctx->vpipe;
359
360 if (!CreateOrResizeBackBuffer(context_priv->vctx, width, height, &context_priv->backbuffer))
361 return BadAlloc;
362
363 vpipe->render_picture(vpipe, surface_priv->pipe_vsfc, PictureToPipe(flags), &src_rect,
364 context_priv->backbuffer, &dst_rect, surface_priv->disp_fence);
365
366 vl_video_bind_drawable(context_priv->vctx, drawable);
367
368 vpipe->screen->flush_frontbuffer
369 (
370 vpipe->screen,
371 context_priv->backbuffer,
372 vpipe->priv
373 );
374
375 return Success;
376 }
377
378 Status XvMCGetSurfaceStatus(Display *dpy, XvMCSurface *surface, int *status)
379 {
380 assert(dpy);
381
382 if (!surface)
383 return XvMCBadSurface;
384
385 assert(status);
386
387 *status = 0;
388
389 return Success;
390 }
391
392 Status XvMCDestroySurface(Display *dpy, XvMCSurface *surface)
393 {
394 XvMCSurfacePrivate *surface_priv;
395
396 assert(dpy);
397
398 if (!surface || !surface->privData)
399 return XvMCBadSurface;
400
401 surface_priv = surface->privData;
402 pipe_video_surface_reference(&surface_priv->pipe_vsfc, NULL);
403 FREE(surface_priv);
404 surface->privData = NULL;
405
406 return Success;
407 }
408
409 Status XvMCHideSurface(Display *dpy, XvMCSurface *surface)
410 {
411 assert(dpy);
412
413 if (!surface || !surface->privData)
414 return XvMCBadSurface;
415
416 /* No op, only for overlaid rendering */
417
418 return Success;
419 }