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